我正在尝试制作像评论部分的Facebook,用户可以在主页上的特定帖子中发表评论。问题在于,如果我尝试在一篇文章中发表评论,那么评论会被循环到所有帖子中。这里是图片:
这是php代码
$get_posts = "SELECT * FROM science_post WHERE user_id ORDER by 1 DESC LIMIT 5";
$run_posts = mysqli_query($conncate, $get_posts);
while ($row_posts = mysqli_fetch_array($run_posts)) {
/** .. Some other code .. ***/
$user_com = $_SESSION['sess_user'];
$get_com = "select * from users where user_name='$user_com'";
$run_com = mysqli_query($conn, $get_com);
$row_com = mysqli_fetch_array($run_com);
$user_com_id = $row_com['user_id'];
$user_com_name = $row_com['user_name'];
if (isset($_POST['submit_co'])) {
if (!empty($_POST['comment_co'])) {
$comment = $_POST['comment_co'];
$insert = "insert into science_comment (post_id,user_id,comment,comment_author,date) values ('$post_id','$user_com_id','$comment','$user_com_name', NOW())";
$run = mysqli_query($conncate, $insert);
} else {
echo "<script> alert('Please enter a answer before submitting.')</script>";
}
}
答案 0 :(得分:0)
评论通常有父ID,从我看到的,您似乎是选择特定用户的所有评论,而不是特定消息,因此您将再次看到相同的评论。
尝试这样的事情:
SELECT * FROM science_comment WHERE post_id = id ORDER by id DESC LIMIT 5