我已經在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