2013-12-18 72 views
0

我已经在php中创建了评论 - 回复系统。它与Facebook中的墙相似。用户写评论,然后将其发布到“墙上”。我在我的数据库中使用下面的表来保存评论:评论(comments_id,评论,评论日期,用户,评论,哈希,闪光)和表用户持有用户的详细信息:用户(user_id,姓名)。一切都很完美,唯一的问题是我无法删除某个评论。删除评论意味着在我的数据库中为此注释设置flag = 1。删除评论 - 回复系统中的某个评论在php中

在每条评论上都有一个名为“delete”的链接。当用户按删除时,一个灯箱以javascript和用户通过按下delete开始,执行函数“deletepost”。我唯一的问题是,这个函数将flag = 1设置为数据库中的所有注释,而不是我按下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(); 
} 
?> 

我使用下面的函数,以删除评论。删除评论意味着我设置标志= 1,并在我的功能,显示评论(功能getComments),如果标志等于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..."); 
} 
?> 
+2

[请不要在新代码中使用'mysql_ *'函数](http://stackoverflow.com/q/12859942)。他们不再维护[并且被正式弃用](http://php.net/mysql_connect)。请改为了解[准备好的语句](http://en.wikipedia.org/wiki/Prepared_statement),并使用[PDO](http://php.net/pdo)或[MySQLi](http:// php。 net/mysqli) - [这篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)将帮助你决定哪个。 [这是一个很好的PDO教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 – vascowhite

+0

我不知道......谢谢 – user2491321

+0

你的数据库模式是什么样的? – user1618143

回答

0

我的第一本能是猜测无论出于何种原因,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库,但我不认为改变这将解决你的问题在这里。

+0

我已经尝试过了,但仍然将flag = 1设置为表中的所有注释。我看不出哪里是错误,这使得它适用于所有,而不是为某些评论 – user2491321

+0

嗯......你确定'comment_id'正确初始化吗?如果您忘记将其设置为“AUTO_INCREMENT”,则可能会发生这样的情况。 – user1618143

+0

是的,我拥有它AUTO_INCREMENT。我在我的数据库中看到我的表格,并且我看到comments_id为每个帖子获取不同的值。 – user2491321

0

在deletepost为什么你运行while循环来获得散列,如果你一次删除一个注释。另一件事是,标志= 1发生在您的所有评论中,因为散列可能是该用户所有评论的常见情况。您需要为特定用户的每条评论创建唯一的哈希值。

+1

任何想法写什么来改善这一点? – user2491321

+0

无论何时插入对特定用户的评论,都会在数据库中插入带有唯一哈希码的评论(如将哈希码作为随机数),同时删除针对特定用户通行证的评论,即用户标识和随机数在函数参数中并且尊重用户标识和数字更新标志。 – Ron