正在尝试 post 从数据库中获取对 "posts" 的评论,但它只是 post 对一个 post 发表评论

Trying to post comments to "posts" fetched from the database but it is only posting comment to only one post

我有一个 table 用于我的数据库中的 post 和另一个 table 用于评论,我想做的是我正在获取我所有的 posts 从数据库和每个 post,我给了一个表格来提交评论。当我尝试 post 使用此表单和 ajax 发表评论时,它仅适用于一个 post 而不适用于其他人。(在这里我想提一下,因为此表单是自动填充的每个 post,它的文本区域都有相同的 ID,可能会导致错误)但我想不通,如何解决这个问题?任何帮助或建议将不胜感激。

(注:我不是专家,只是学习者。)

这是我的代码:

    <?php
echo '<div class="'.$row['post_id'].'">';            

$connect = mysqli_connect("localhost", "root", "", "testing") or die("<p><span style=\"color: red;\">Unable to select database</span></p>");

$post_id = $row['post_id'];

$resultt = mysqli_query($connect,"SELECT * FROM tbl_comment WHERE parent_comment_id = '0' AND post_id = '$post_id' ORDER BY comment_id DESC ");
while($rowss = mysqli_fetch_array($resultt, MYSQLI_BOTH))
{
if ($rowss>0){
echo '
 <div>
  <div>'.$rowss["comment"].'</div>
  <div style="font-size: 12px; font-family: serif;">By <b>'.$rowss["comment_sender_name"].'</b> on '.$rowss["date"].'</div>
  <div align="right" style="margin-bottom: 19px;"><a href="#" class="reply" id="'.$rowss["comment_id"].'">Reply</a></div>
 </div>
 ';
} else { echo ''; } }

echo '</div>
<form method="POST" class="comment_form">
<div class="form-group"><textarea name="comment_content" class="form-control comment_content" post_id ="'.$row['post_id'].'" placeholder="Enter Comment" rows="2"></textarea></div>
    <div class="form-group">
     <input type="submit" name="submit" class="btn btn-info" value="Publish" onClick="adcomnt()" />
    </div>
   </form>

 <script>

                    function adcomnt() {
                    var post_id = $(".comment_content").attr("post_id");
                    var comment_name = '.$_SESSION['username'].';   
                    var comment_content = $(".comment_content").val();
                    var comment_id = 0;

                    $.ajax({
                        type: "POST",
                        url: "add_comment.php",
                        dataType: "json",
                        data: "post_id=" +post_id+ "&comment_content=" + comment_content+ "&comment_id=" + comment_id+ "&comment_name=" + comment_name, 
                        success: function(data) {
                if(data.status == "error"){
                alert("Oops, Comment not inserted!");
                } 
                else if(data.status == "success"){
                alert("Thank you! comment inserted!");


                }},
                    });


}

</script>

            </div>';
            ?>

我不确定,但我认为问题出在 javascript 变量 "post_id" 和 "comment_content" 上,它们看起来没有链接到文本区域。

所以你可以尝试这样的事情:

    <?php
    echo '<div class="'.$row['post_id'].'">';            

    $connect = mysqli_connect("localhost", "root", "", "testing") or die("<p><span style=\"color: red;\">Unable to select database</span></p>");

    $post_id = $row['post_id'];

    $resultt = mysqli_query($connect,"SELECT * FROM tbl_comment WHERE parent_comment_id = '0' AND post_id = '$post_id' ORDER BY comment_id DESC ");
    while($rowss = mysqli_fetch_array($resultt, MYSQLI_BOTH))
    {
    if ($rowss>0){
    echo '
     <div>
      <div>'.$rowss["comment"].'</div>
      <div style="font-size: 12px; font-family: serif;">By <b>'.$rowss["comment_sender_name"].'</b> on '.$rowss["date"].'</div>
      <div align="right" style="margin-bottom: 19px;"><a href="#" class="reply" id="'.$rowss["comment_id"].'">Reply</a></div>
     </div>
     ';
    } else { echo ''; } }

    echo '</div>

    <div class="form-group"><textarea name="comment_content" class="form-control comment_content" id="content_'.$row['post_id'].'" placeholder="Enter Comment" rows="2"></textarea></div>
        <div class="form-group">
         <input type="submit" id="submit_comment" post_id="'.$row['post_id'].'" name="submit" class="btn btn-info" value="Publish" onClick="adcomnt()" />
        </div>


     <script>
   $(document).ready(function()
{
    $( "#submit_comment" ).click(function(e) { 

    e.preventDefault(); 

    var post_id = $(this).attr("post_id");
    var comment_name = '.$_SESSION['username'].';   
    var comment_content = $("#content_"+post_id).val();
    var comment_id = 0;

    $.ajax({
        type: "POST",
        url: "add_comment.php",
        dataType: "json",
        data: "post_id=" +post_id+ "&comment_content=" + comment_content+ "&comment_id=" + comment_id+ "&comment_name=" + comment_name, 
        success: function(data) {
            if(data.status == "error"){
            alert("Oops, Comment not inserted!");
            } 
            else if(data.status == "success"){
            alert("Thank you! comment inserted!");


            }},
        });

    });

});
    </script>

                </div>';
                ?>

问题已解决,通过进行一些更改,因为它是一个循环内的表单,为每个元素提供了一个唯一的 ID:

这是更新后的代码:

    <form method="POST" class="comment_form" id="f'.$post_id.'">
        <div class="form-group"><textarea name="comment_content" class="form-control comment_content" id="content_'.$post_id.'" placeholder="Enter Comment" rows="2"></textarea></div>
            <div class="form-group">
              <input type="submit" id="submit_comment'.$post_id.'" post_id="'.$post_id.'" name="submit" class="btn btn-info" value="Publish" />
            </div></form>

<script>
       $(document).ready(function(){
        $( "#submit_comment'.$post_id.'" ).click(function(e) { 
        e.preventDefault()

现在一切正常。