我在php中创建了一个评论回复系统。它类似于facebook中的墙。用户编写评论然后将其发布在“墙”中。我在我的数据库中使用以下表来保存注释:注释(comments_id,comment,comment_date,user,comment_hash,flash)以及保存用户详细信息的表用户:用户(user_id,name,姓)即可。一切都很完美,唯一的问题是我无法删除某个评论。删除注释意味着在我的数据库中为此注释设置flag = 1。
在每条评论上都有一个名为“删除”的链接。当用户按下删除时,通过按下删除,在javascript和用户中启动灯箱,执行“deletepost”功能。我唯一的问题是这个函数将flag = 1设置为我的数据中的所有注释,而不是我按删除的某些注释。知道如何改进我的代码吗?
我使用以下函数来显示注释:
<?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...");
}
?>
答案 0 :(得分: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库,但我不认为改变它会解决你的问题。
答案 1 :(得分:0)
在deletepost中,为什么你运行while循环来获取哈希,如果你一次删除一个注释。另一件事是你的所有评论都会发生flag = 1,因为哈希可能对那些用户的所有评论都很常见。您需要为特定用户的每个注释创建唯一的哈希值。