2011-06-05 126 views
0

我正在從表中刪除一行(用戶)帳戶,我試圖設置值「跟隨計數」和「跟隨者計數」爲其值減一。但由於某種原因,這沒有發生。該帳戶成功刪除,但減量不會發生。MySQL行在刪除後沒有更新

請你能告訴我什麼,我做錯了:

$query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'"); 
    if (mysql_num_rows($query) < 1) { 
     exit("Account doesn't exist"); 
    } 
    $row = mysql_fetch_assoc($query); 
    $id = $row["id"]; 
    $query = NULL; 
    mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'"); 
    mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'"); 
    mysql_query("DELETE FROM `Posts` WHERE `accountID` = '$id'"); 
    mysql_query("DELETE FROM `Accounts` WHERE `id` = '$id'"); 
    $arg = mysql_query("SELECT * FROM Following WHERE followingUserID = '$id'"); 
    if (mysql_num_rows($arg) >= 1) { 
     for ($i = 0; $i < mysql_num_rows($arg); $i++) { 
      $arr = mysql_fetch_assoc($arg); 
      $followingUserID = $arr['followingUserID']; 
      $followedUserID = $arr['followedUserID']; 
      $art = mysql_fetch_assoc(mysql_query("SELECT `Following Count` FROM Accounts WHERE `id` = '$followedUserID'")); 
      $followingCount = $art['Following Count']; 
      $followingCount = $followingCount-1; 
      $arts = mysql_fetch_assoc(mysql_query("SELECT `Follower Count` FROM Accounts WHERE `id` = '$followingUserID'")); 
      $followedCount = $arts['Followed Count']; 
      $followedCount = $followedCount-1; 
      mysql_query("UPDATE Accounts SET `Following Count` = '$followingCount' WHERE `id` = '$followingUserID'"); 
      mysql_query("UPDATE Accounts SET `Follower Count` = '$followedCount' WHERE `id` = '$followedUserID'"); 
      mysql_query("DELETE FROM Following WHERE followingUserID = '$id'"); 
     } 
    } 

    exit("Closed"); 
+1

像這樣使用mysql_query:mysql_query(..)或die(mysql_error());你會發現你的錯誤 – 2011-06-05 21:07:36

+0

請告訴我你在這裏消毒你的輸入 - 你的代碼中還存在競爭條件;您可以使用SQL直接遞增或遞減數字列一定的數量,這將防止此問題在路上發生 – damianb 2011-06-05 21:09:25

+0

我只想知道是否有任何明顯的錯誤會阻止我創建新用戶,請按照另一個用戶並且每次刪除用戶直到它工作。 – 2011-06-05 21:09:50

回答

3

爲什麼不能簡單地做

mysql_query("UPDATE Accounts SET `Following Count` = (`Following Count` - 1) WHERE `id` = '$followingUserID'"); 

mysql_query("UPDATE Accounts SET `Follower Count` = (`Following Count` - 1) WHERE `id` = '$followedUserID'"); 

這種方式,你不會需要2種選擇。

+0

+1,避免了我剛纔指出的競爭條件 – damianb 2011-06-05 21:10:31