MySQL 不减少值

发布于 2024-11-14 12:30:48 字数 1364 浏览 3 评论 0原文

当我尝试删除帖子时,无法使帐户表中的值递减。我想减少的值是“PostCount”、“Likes Count”和“CommentsCount”。目前只有“PostCount”有效。

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

$arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

    $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
    while ($row = mysql_fetch_assoc($query)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
    $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
    while($row = mysql_fetch_assoc($arg)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
    while ($row = mysql_fetch_assoc($arg)) {
        mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
    }
    mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
    exit("Deleted post");

I can't get the values in the table Accounts to decrement when I try to delete a post. The values I would like to decrement are "PostCount", "Likes Count" and "CommentsCount". Currently only the "PostCount" works.

Please can you tell me what I am doing wrong:

$arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

    $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
    while ($row = mysql_fetch_assoc($query)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
    $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
    while($row = mysql_fetch_assoc($arg)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
    while ($row = mysql_fetch_assoc($arg)) {
        mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
    }
    mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
    exit("Deleted post");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-11-21 12:30:48

首先,循环查询会浪费你的执行时间。您希望将其限制为尽可能少的查询,这就是 implode 的用武之地play:

// what is this for? I do not see it being used. $arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
while ($row = mysql_fetch_assoc($query)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` IN(" . implode(',', $b) . ")") or trigger_error('MySQL Update Failed: ' . mysql_error());

这会更有效率并且具有相同的效果。

下一个查询应该类似:

$arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
// again an extra unnecessary query not being used. $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
$b=array();
while($row = mysql_fetch_assoc($arg)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` IN(".implode(',', $b) . ")") or trigger_error(mysql_error());

下一个查询,我什至不知道您为什么要循环它:

//$arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
//while ($row = mysql_fetch_assoc($arg)) {
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'") or trigger_error(mysql_error());
//}

由于您没有在任何地方使用该数据,因此只需运行更新查询即可。

实施上述内容应该可以加快您的应用程序并减少冗余。 implode 非常方便,运行 1 个查询与运行多个查询几乎总是首选。我不确定这是否能解决您的问题,但这是朝着修复代码的正确方向迈出的一大步。

First up looping queries kills your execution time. You want to limit it to as little queries as possible, this is where implode comes into play:

// what is this for? I do not see it being used. $arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
while ($row = mysql_fetch_assoc($query)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` IN(" . implode(',', $b) . ")") or trigger_error('MySQL Update Failed: ' . mysql_error());

This will be much more efficient and have to same effect.

The next query, should be similar:

$arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
// again an extra unnecessary query not being used. $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
$b=array();
while($row = mysql_fetch_assoc($arg)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` IN(".implode(',', $b) . ")") or trigger_error(mysql_error());

This next one, I do not even know why you are looping through it:

//$arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
//while ($row = mysql_fetch_assoc($arg)) {
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'") or trigger_error(mysql_error());
//}

As you are not utilizing that data anywhere, just run the update query.

Implementing the above should speed up your application and reduce redundancy. implode is very handy and running 1 query vs several is almost always preferred. I am not sure if that will solve your problem, but it is a huge step in the right direction in fixing your code.

梦毁影碎の 2024-11-21 12:30:48

我重新编码了包含我发布的代码的 if 语句,它现在工作正常:

mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
        $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'") or die(mysql_error());
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `Likes Count` = (`Likes Count` - 1) WHERE `id` = '$accID'");
        }
        $query = mysql_query("SELECT * FROM Comments WHERE `postID` = '$postID'");
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$accID'");
        }
        mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
        exit("Deleted post");

I re-coded my if statement containing the code I posted and it is now working perfectly:

mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
        $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'") or die(mysql_error());
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `Likes Count` = (`Likes Count` - 1) WHERE `id` = '$accID'");
        }
        $query = mysql_query("SELECT * FROM Comments WHERE `postID` = '$postID'");
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$accID'");
        }
        mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
        exit("Deleted post");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文