【发布时间】:2013-12-18 18:54:05
【问题描述】:
我在 php 中创建了一个评论回复系统。它类似于 facebook 中的墙。用户撰写评论,然后将其发布在“墙”中。我在我的数据库中使用以下表来保存 cmets:cmets(cmets_id, comment, comment_date, user, comment_hash, flash) 和保存用户详细信息的表 users:users(user_id, name,姓)。一切都很完美,唯一的问题是我无法删除某个评论。删除评论意味着在我的数据库中为此评论设置 flag=1。
每条评论都有一个名为“删除”的链接。当用户按下delete时,一个灯箱在javascript中启动,用户按下delete,函数“deletepost”被执行。我唯一的问题是此函数将 flag=1 设置为我的数据库中的所有 cmets,而不是为我按下删除的某些评论。知道如何改进我的代码吗?
我使用以下函数来显示 cmets:
<?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;
}
?>
我使用以下函数来发布 cmets:
<?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();
}
?>
我使用以下函数来删除 cmets。删除 cmets 意味着我设置了 flag=1,并且在显示 cmets 的函数(函数 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...");
}
?>
【问题讨论】:
-
我不知道...谢谢
-
您的数据库架构是什么样的?
-
如果你指的是表格,我已经把它们写在了顶部
-
但是数据类型是什么,索引是什么,你确定你正确设置了主键等等?你能发布你用来创建表的sql命令吗?
标签: javascript php