我在php中创建了一个评论回复系统。它类似于facebook中的墙。用户写一个评论,然后张贴在“墙壁”。我使用数据库中的以下表来保存注释:注释(comments_id、comment、comment_date、user、comment_hash、flash)和保存用户详细信息的表用户:用户(user_id,name,姓氏)。一切都很完美,唯一的问题是我不能删除某个评论。删除注释意味着在我的数据库中为这个注释设置flag=1。
在每个评论中都有一个名为“删除”的链接。当用户按delete时,一个灯光框在javascript中开始,用户通过按delete键执行“删除”函数。我唯一的问题是,这个函数将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();
}
?>我使用以下函数来删除注释。删除注释意味着我设置了flag=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...");
}
?>发布于 2013-12-18 19:09:50
我的第一本能是猜测,无论出于什么原因,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...");
}我不知道您当前的delete函数为什么要查询数据库以从表中获取哈希,然后使用哈希从同一表中查找同一行。这似乎是毫无意义和低效的,并带来了更多的东西,可以打破。
顺便说一句,Vascowhite是正确的,您不应该使用旧的mysql库,但我不认为修改它会解决您的问题。
发布于 2013-12-18 19:12:41
在删除文章中,如果您只删除一次注释,为什么要运行while循环来获取哈希。另一件事是,flag=1会发生在您的所有评论中,因为散列对于所有评论的用户来说都很常见。您需要使散列对于特定用户的每一个注释都是唯一的。
https://stackoverflow.com/questions/20666153
复制相似问题