2016-12-29 148 views
-2

我想做一个像评论部分的Facebook,用户可以在主页的特定帖子发表评论。问题是,如果我尝试在一篇文章中发表评论,那么评论就会卷入所有帖子。这里的形象:php Facebook like评论部分

enter image description here

这里是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

评论通常有母公司的ID,从我所看到的,你似乎是由特定用户选择所有的意见,而不是通过一个特定的消息,因此你会再次看到相同的评论。

尝试这样:

SELECT * FROM science_comment WHERE post_id = id ORDER by id DESC LIMIT 5 
+0

感谢您的答复,但问题是,它在数据库表的循环,以及。在science_comment表中,我有10条评论与不同的评论ID和帖子ID。 –