在PHP的注释-回复系统中删除某个注释

delete a certain comment in comment-reply system in php

本文关键字:注释 删除 系统 回复 PHP      更新时间:2023-09-26

我在php中创建了一个注释-回复系统。它类似于Facebook中的墙壁。用户写评论,然后将其发布在"墙"中。我在数据库中使用以下表来保存注释:注释(comments_id,注释,comment_date,用户,comment_hash,flash)和保存用户详细信息的表用户:用户(user_id,姓名,姓氏)。一切都很完美,唯一的问题是我无法删除某个评论。删除注释意味着在我的数据库中为此注释设置 flag=1。

每条评论上都有一个名为"删除"的链接。当用户按下删除时,一个灯箱在javascript中启动,用户通过按删除,执行"删除帖子"功能。我唯一的问题是这个函数将 flag=1 设置为我的 databe 中的所有注释,而不是我按 Delete 的某个注释。知道如何改进我的代码吗?

我使用以下函数来显示注释:

<?php
function getComments(){    
  $session_user_id = $_SESSION['user_id'];
  $comments = "";
  $sql = mysql_query("SELECT * FROM comments WHERE (`flag`=0) ORDER BY comment_date DESC LIMIT 40") or die (mysql_error());
  if(mysql_num_rows($sql) == 0){
    $comments = "<div class='each_comment'>  Write your first posts ...</div> ";
  }
  else{
    while ($row= mysql_fetch_assoc($sql)) {
  $comment_id = $row['comments_id'];
      $hash = $row['comment_hash'];
      $personal_1 = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE `user_id`='{$row['user']}' ");
        while ($run_personal_1= mysql_fetch_assoc($personal_1)) {
          $comment_user_id = $run_personal_1['user_id'];
          $comment_user_name = $run_personal_1['name'];
          $comment_user_surname = $run_personal_1['surname'];
        }
    // displays comment that includes user's name and surname and hash
    $comments .= " $comment_user_surname   $comment_user_name   $hash";
    $comments .= ".$row['comment'].";

//---- at this point I insert a delete link , that when user presses it a javascript light box ask user if wants to delete the comment. If user press the delete button it is called the function named "deletepost".
//---- first checks if the comment is from the user that is logged in ($session_user_id) in order to have the right to delete post
  if($comment_user_id == $session_user_id){
      if(isset($_POST['submit_2'])) {
        deletepost($session_user_id, $comment_id);
        header('Location: wall.php');
      } 
  $comments .= <<<EOD
  <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"> <font color='grey' >Delete</font> </a>
<div id="light" class="white_content">
    <form action="$_SERVER[PHP_SELF]" method="post">
    <input type="submit" name="submit_2" value="Delete Post ">
    </form>
    <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"><button>Cancel</button></a>
</div>
<div id="fade" class="black_overlay"></div>             
  EOD;
  }
  }
    return $comments;   
} 
?>

我使用以下功能来发表评论:

<?php
function postComments($comment){
    $comment = mysql_real_escape_string(strip_tags($comment));
        $session_user_id = $_SESSION['user_id'];
        $random_num = rand(0, 99999999999);
        $sql = mysql_query(" INSERT INTO `comments` (comment, comment_date, user, comment_hash) VALUES ('".$comment."', now(), '$session_user_id', '$random_num') ");
    return getComments();
}
?>

我使用以下函数来删除评论。删除注释意味着我设置 flag=1,并且在显示注释的函数(函数 getComments)中,如果 flag 等于 1,则我不显示此注释:

<?php
function deletepost($comment_user_id, $comment_id){
$get_hash = mysql_query("SELECT `comment_hash` from `comments` WHERE (`user`='$comment_user_id' AND `comments_id` = '$comment_id')  ");
        while ($run_hash= mysql_fetch_assoc($get_hash)) {
            $hash = $run_hash['comment_hash'];
        }
    $sql="UPDATE `comments` SET `flag`=1 WHERE (`user`='$comment_user_id' AND `comment_hash`='$hash')";
$result=mysql_query($sql) or die("Error when trying to delete...");
}
?>

我的第一直觉是猜测comment_hash工作不太正确,无论出于何种原因。尝试简化删除功能:

function deletepost($comment_user_id, $comment_id){
    $sql="UPDATE `comments` SET `flag`=1 WHERE (`user`='$comment_user_id' AND `comments_id`='$comment_id')";
    $result=mysql_query($sql) or die("Error when trying to delete...");
}

我不确定为什么您当前的删除函数查询您的数据库以从表中获取哈希,然后使用哈希从同一表中查找同一行。它似乎毫无意义且效率低下,并引入了更多可能中断的东西。

顺便说一句,Vascowhite 是正确的,你不应该使用旧的 mysql 库,但我认为改变它会解决你的问题。

在 deletepost 中,为什么要运行 while 循环来获取哈希值,如果你删除一条评论一次.另一件事是 flag=1 发生在您的所有评论中,因为哈希对于该用户的所有评论来说可能是常见的。您需要为特定用户的每条评论提供唯一的哈希值。