同样的评论显示了多个PHP网页

问题描述 投票:-1回答:1

我有一个多文章的网页和每一篇文章我已经把文章下方的评论系统,使人们可以对文章的评论什么。

问题是,每当我做任何物品,其评论表明我对别人的文章同样的评论。

所以人们请帮我out.and在此先感谢。

 
      <!--Javascript of Comment System -->
<script>
$(document).ready(function(){
 
 $('#comment_form').on('submit', function(event){
  event.preventDefault();
  var form_data = $(this).serialize();
  $.ajax({
   url:"add_comment.php",
   method:"POST",
   data:form_data,
   dataType:"JSON",
   success:function(data)
   {
    if(data.error != '')
    {
     $('#comment_form')[0].reset();
     $('#comment_message').html(data.error);
     $('#comment_id').val('0');
     load_comment();
    }
   }
  })
 });

 load_comment();

 function load_comment()
 {
  $.ajax({
   url:"fetch_comment.php",
   method:"POST",
   success:function(data)
   {
    $('#display_comment').html(data);
   }
  })
 }
  
 $(document).on('click', '.reply', function(){
  var comment_id = $(this).attr("id");
  $('#comment_id').val(comment_id);
  $('#comment_name').focus();
 });
 
});
</script>
	<!--comment system script and css stylesheet-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <!--This is the comment container-->
<p class="container"><h3>Ask A Question</h3></p>
<br>

    <!--This is the comment system-->
  <form method="POST" id="comment_form">
  <div class="form-group">
  <input type="text" name="comment_name" id="comment_name" class="form-control" placeholder="Enter Name" />
    </div>
    <div class="form-group">
     <textarea name="comment_content" id="comment_content" class="form-control" placeholder="Enter Comment" rows="5" ></textarea>
    </div>
    <div class="form-group">
     <input type="hidden" name="comment_id" id="comment_id" value="0" />
     <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
    </div>
   </form>
   <span id="comment_message"></span>
   <br />
   <div id="display_comment"></div>
  </div>

</div>

fetch_comment.php

add_comment.php

javascript php
1个回答
1
投票

看看你load_comment功能:

 function load_comment()
 {
     $.ajax({
         url:"fetch_comment.php",
         method:"POST",
         success:function(data)
         {
             $('#display_comment').html(data);
         }
     })
 }

有一篇文章编号参考。从本质上讲,你发送的评论的请求没有从文章的评论应该是未来指定。现在,让我们说你做修复,通过增加一个的article_id的请求:

 function load_comment()
 {
     $.ajax({
         url:"fetch_comment.php",
         data: {article_id: 0},
         method:"POST",
         success:function(data)
         {
             $('#display_comment').html(data);
         }
     })
 }

你仍然会得到相同的意见,结果。这是因为你的fetch_comment.php脚本不具备的功能能够通过article_id指定的意见。看看fetch_comment.php的前几行:

$connect = new PDO('mysql:host=localhost;dbname=techotub_testing', 'techotub_testing', 'password');

$query = "
    SELECT * FROM tbl_comment 
    WHERE parent_comment_id = '0' 
    ORDER BY comment_id DESC
";

$statement = $connect->prepare($query);

$statement->execute();

在这里,我们可以看到,您要求从tbl_comment所有的意见,没有指定的文章。你想要做的,为了从一个特定的文章所有评论什么,是添加一行到指定您所请求的文章where条款。如果您传递的article_id通过电话后使用Ajax,您可以通过使用$_POST['article_id']做到这一点

不幸的是,你将无法使用任何一种物品的过滤,但因为你插入SQL语句:

$query = "
 INSERT INTO tbl_comment 
 (parent_comment_id, comment, comment_sender_name) 
 VALUES (:parent_comment_id, :comment, :comment_sender_name)
 ";

这就告诉我们,你是不是保存文章编号放在桌子上,当你添加一个新评论。如果你想通过文章来过滤,你需要有一个物品栏,并指定每个插入的ID。一旦你这样做,你应该能够开始编辑你的代码的其余部分文章进行筛选。

tldr

  1. 添加文章ID列到表
  2. 开始添加新的注释时有价值物品ID插入到表
  3. 添加一个新行到SELECT子句的文章编号,以显示
  4. 从您的AJAX功能通过文章编号,以你的PHP脚本
© www.soinside.com 2019 - 2024. All rights reserved.