Nadodouble

loktoy

Established
pano ko po kaya sya mafifix kasi nadodouble yun output nya o display sakali naman lagyan ko ng LIMIT 1 tapos gusto ko ulit mag reply dina sya nagdidisplay ulit

<div class="container col-md-6 bg-body-tertiary border mt-3 rounded-3" >
<form method="POST">
<input type="hidden" name="id" value="<?php echo $username; ?>">
<label class="fw-bold mb-2" for="w3review">Comment:</label>
<textarea name="comment_content" class="form-control" rows="4" cols="40" placeholder="Your Content....."></textarea>
<div class="form-group mb-2 mt-2" style="text-align: right">
<input type="submit" name="comment" value="Reply" class="w-25" style="background-color:#0D52C7;">
</div>
</form>
</div>


<?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 * FROM comment LEFT JOIN registration on registration.user_id = comment.user_id") or die (mysqli_error());
while($comment_row = mysqli_fetch_array($comment_query)){
$comment_id = $comment_row['comment_id'];
$comment_by = $comment_row['username'];
?>

<div class="container col-md-6 bg-body-tertiary border rounded-3" >
<a style="text-decoration:none; float:left;" href="deletepost.php<?php echo '?id='.$id; ?>">
<div class="pt-3">

<h5>Posted by: <a href="#" style= "margin-left:10px; text-decoration:none;"> <?php echo $comment_by; ?></a></h5>
<h6><?php echo $comment_row['comment']; }}?></h6>
 
To fix the issue of the output being duplicated, you can add a LIMIT clause to your SQL query to limit the number of rows returned. Also, make sure to reorder your code so that the LIMIT clause is applied before the ORDER BY clause.

Here's an example of how you can modify your code:

PHP:
$comment_query = mysqli_query($con, "SELECT * FROM comment LEFT JOIN registration ON registration.user_id = comment.user_id ORDER BY comment.date_posted DESC LIMIT 1") or die(mysqli_error());

In the example above, I added the ORDER BY comment.date_posted DESC clause to sort the comments in descending order based on the date posted. Then, I added the LIMIT 1 clause to only retrieve the latest comment. You can adjust the LIMIT value according to your needs.

By doing this, you will only get the latest comment in the result set, and it should no longer display duplicate comments.

Additionally, make sure to properly close your HTML tags in your code to avoid any rendering issues.
 

About this Thread

  • 1
    Replies
  • 336
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
1,018
Guests online
1,304
Total visitors
2,322

Forum statistics

Threads
2,276,213
Posts
28,968,356
Members
1,231,168
Latest member
nrking
Back
Top