2013-12-18 70 views
0

我已經在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..."); 
} 
?> 
+2

[請不要在新代碼中使用'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

+0

我不知道......謝謝 – user2491321

+0

你的數據庫模式是什麼樣的? – user1618143

回答

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庫,但我不認爲改變這將解決你的問題在這裏。

+0

我已經嘗試過了,但仍然將flag = 1設置爲表中的所有註釋。我看不出哪裏是錯誤,這使得它適用於所有,而不是爲某些評論 – user2491321

+0

嗯......你確定'comment_id'正確初始化嗎?如果您忘記將其設置爲「AUTO_INCREMENT」,則可能會發生這樣的情況。 – user1618143

+0

是的,我擁有它AUTO_INCREMENT。我在我的數據庫中看到我的表格,並且我看到comments_id爲每個帖子獲取不同的值。 – user2491321

0

在deletepost爲什麼你運行while循環來獲得散列,如果你一次刪除一個註釋。另一件事是,標誌= 1發生在您的所有評論中,因爲散列可能是該用戶所有評論的常見情況。您需要爲特定用戶的每條評論創建唯一的哈希值。

+1

任何想法寫什麼來改善這一點? – user2491321

+0

無論何時插入對特定用戶的評論,都會在數據庫中插入帶有唯一哈希碼的評論(如將哈希碼作爲隨機數),同時刪除針對特定用戶通行證的評論,即用戶標識和隨機數在函數參數中並且尊重用戶標識和數字更新標誌。 – Ron