我已经在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...");
}
?>
[请不要在新代码中使用'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
我不知道......谢谢 – user2491321
你的数据库模式是什么样的? – user1618143