What's new

Anu kaya solution ko dito

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where comment_id = '495'' at line 1 in C:\xampp\htdocs\Innerjoin\user\postinsert.php:96 Stack trace: #0 C:\xampp\htdocs\Innerjoin\user\postinsert.php(96): mysqli_query(Object(mysqli), 'SELECT * ,UNIX_...') #1 {main} thrown in C:\xampp\htdocs\Innerjoin\user\postinsert.php on line 96
ito code ko

<?php
ob_start();
// include navigation with connection and session
include "../partials/newnav.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Posted</title>
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet">
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet">
<script src="You do not have permission to view the full content of this post. Log in or register now."></script>
<link href="You do not have permission to view the full content of this post. Log in or register now." rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="You do not have permission to view the full content of this post. Log in or register now." integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="You do not have permission to view the full content of this post. Log in or register now." integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
<script src="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</head>
<body>
<?php
// insert to post database
if (isset($_POST['post'])) {
$page_content = $_POST['post_content']; //get the input text
$pagecontent = $_POST['postcontent']; //get the input title
$date_created = strtotime(date("Y-m-d h:i:sa")); //get the date and time
// prevent to ****ing sql injection
$slq = mysqli_prepare($con, "INSERT INTO post (comment,title, date_created, user_id) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($slq, "sssi", $page_content, $pagecontent, $date_created, $user_id);
mysqli_stmt_execute($slq);
header('location:postinsert.php');
exit();
ob_end_flush();
}
?>
<?php
// display query from post database
$post_query = mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_created as TimeSpent FROM post left join registration on registration.user_id = post.user_id order by comment_id DESC limit 1 ") or die(mysqli_error($con));
// loop
while ($post_row = mysqli_fetch_array($post_query)){
$id = $post_row['comment_id']; //get the comment_id
$uid = $post_row['user_id']; //get the user_id
$postedby = $post_row['username']; //get the username
?>
<!-- body of posted -->
<div class="container col-md-6 bg-body-tertiary border rounded-3">
<h5>Posted by: <a href="#" style= "margin-left:10px; text-decoration:none;"> <?php echo $postedby; ?></a></h5>
<!-- time interval -->
<?php
$days = floor($post_row['TimeSpent'] / (60 * 60 * 24));
$remainder = $post_row['TimeSpent'] % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0)
echo date('F d, Y - H:i:sa', $post_row['date_created']);
elseif($days == 0 && $hours == 0 && $minutes == 0)
echo "A few seconds ago";
elseif($days == 0 && $hours == 0)
echo $minutes.' minutes ago';
?>
<img src="../img/hour.png" alt= "hour logo" style = "width:20px";>
<h1><?php echo $post_row['comment']; ?></h1>
<?php ?>
</div>
<form>
Comment:<br>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<textarea name="comment_content" class="form-control" rows="2" cols="44" style="" placeholder=".........Type your comment here........" required></textarea><br>
<input type="submit" name="comment">
</form>
</div>
</br>
<?php

if (isset($_POST['comment'])){
$comment_content = $_POST['comment_content'];
$post_id=$_POST['id'];

mysqli_query($con,"insert into comment (comment,date_posted,user_id,comment_id) values ('$comment_content','".strtotime(date("Y-m-d h:i:sa"))."','$user_id','$post_id')") or die (mysqli_error());

}
?>

<?php
$comment_query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id order by comment.date_posted DESC where comment_id = '$id' ") or die (mysqli_error());
while ($comment_row=mysqli_fetch_array($comment_query)){
$comment_id = $comment_row['id'];
$comment_by = $comment_row['username'];
?>

<br><div class="container col-md-6 bg-body-tertiary border rounded-3"><?php echo $comment_by; ?> - <?php echo $comment_row['comment']; ?>
<br>
<?php }}?>


<div class="mt-2">
<?php
include "../partials/footer.php";
?>
</div>
</body>
</html>
 
The error you're encountering is due to a syntax error in your SQL query. The query is missing the "FROM" clause, which is causing the error. To fix this, you need to modify the line where the query is being executed.

Replace this line:

mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id order by comment.date_posted DESC where comment_id = '$id' ")

with this line:

mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment INNER JOIN registration on registration.user_id = comment.user_id WHERE comment_id = '$id' ORDER BY comment.date_posted DESC")

This modification adds the missing "FROM" clause and corrects the placement of the "WHERE" and "ORDER BY" clauses.

Once you make this change, the SQL query should execute without any syntax errors.
 
The error you're encountering is due to a syntax error in your SQL query. The query is missing the "FROM" clause, which is causing the error. To fix this, you need to modify the line where the query is being executed.

Replace this line:

mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id order by comment.date_posted DESC where comment_id = '$id' ")

with this line:

mysqli_query($con, "SELECT *,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment INNER JOIN registration on registration.user_id = comment.user_id WHERE comment_id = '$id' ORDER BY comment.date_posted DESC")

This modification adds the missing "FROM" clause and corrects the placement of the "WHERE" and "ORDER BY" clauses.

Once you make this change, the SQL query should execute without any syntax errors.
bakit po kaya wala ng display yun ouput dito kapag nag iinput ako

ito code ko

<?php

if (isset($_POST['comment'])){
$comment_content = $_POST['comment_content'];
$post_id=$_POST['id'];

mysqli_query($con,"insert into comment (comment,date_posted,user_id,comment_id) values ('$comment_content','".strtotime(date("Y-m-d h:i:sa"))."','$user_id','$post_id')") or die (mysqli_error());

}
?>

<?php
$comment_query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id where comment_id = '$id' order by comment.date_posted DESC") or die (mysqli_error());
while ($comment_row=mysqli_fetch_array($comment_query)){
$comment_id = $comment_row['id'];
$comment_by = $comment_row['username'];
?>

<br><div class="container col-md-6 bg-body-tertiary border rounded-3"><?php echo $comment_by; ?> - <?php echo $comment_row['comment']; ?>
<br>
<?php }}?>
 
bakit po kaya wala ng display yun ouput dito kapag nag iinput ako

ito code ko

<?php

if (isset($_POST['comment'])){
$comment_content = $_POST['comment_content'];
$post_id=$_POST['id'];

mysqli_query($con,"insert into comment (comment,date_posted,user_id,comment_id) values ('$comment_content','".strtotime(date("Y-m-d h:i:sa"))."','$user_id','$post_id')") or die (mysqli_error());

}
?>

<?php
$comment_query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP() - date_posted AS TimeSpent FROM comment inner JOIN registration on registration.user_id = comment.user_id where comment_id = '$id' order by comment.date_posted DESC") or die (mysqli_error());
while ($comment_row=mysqli_fetch_array($comment_query)){
$comment_id = $comment_row['id'];
$comment_by = $comment_row['username'];
?>

<br><div class="container col-md-6 bg-body-tertiary border rounded-3"><?php echo $comment_by; ?> - <?php echo $comment_row['comment']; ?>
<br>
<?php }}?>
There are a few potential issues with your code that could be causing the lack of display of the output when you input a comment:

1. Make sure you have established a database connection before executing any queries. Ensure that you have included the necessary code to establish a connection to your database.

2. Check that the variables $user_id and $id are being set correctly. Verify that they contain the expected values before executing the query.

3. Double-check the column names in your SQL query. Ensure that the column names used in the query match the actual column names in your database.

4. Confirm that the comment_id value is correctly passed to the script, as it is used in the WHERE clause of the query. Check that the hidden input field with the name id is present in your form and that its value is being sent along with the POST request.

By checking these aspects of your code, you should be able to identify and resolve any issues that may be causing the lack of display of the output.
 

Similar threads

Back
Top