php - MySQL行在删除时不会更新

标签 php 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");

最佳答案

为什么不简单地做

 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选择。

关于php - MySQL行在删除时不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245848/

相关文章:

mysql - 这是使用 mySql 选择与表不同的正确方法吗?

php - 您可以根据 ajax 响应中提交的表单数据提示下载吗?

php - 我将如何在 sql 查询中执行数学运算来计算百分比差异?

php - 需要帮助重构我的冗余代码(使用 MySQL/PHP 来填充/构建 3 个选择列表)

PHP + MySQL : Full Text and Faceted Search with no server side support

mysql - where条件下的执行顺序

php - 把工作交给 MySQL 还是交给 PHP?

php - 在javascript中为数组赋值

php - 动态验证表单数据

PHP MySql 查询显示当前时间而不是平板电脑上的时间