删除时不会更新MySQL行

时间:2011-06-05 21:03:28

标签: php mysql

我正从表Accounts中删除一行(用户),我试图将值“Follow Count”和“Follower Count”设置为它们的值,减去一个。但由于某种原因,这种情况不会发生。帐户成功删除,但不会发生减量。

请你告诉我我做错了什么:

$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 个答案:

答案 0 :(得分: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个选择。