gpt4 book ai didi

php - 使用 id 数组更新和删除多行

转载 作者:行者123 更新时间:2023-11-29 03:48:12 24 4
gpt4 key购买 nike

我不想检查用户团队发布的挑战是否在 1 天后没有被排除,然后自动退还用户团队的积分,然后删除所有挑战。到目前为止,这是我的代码。

//Delete all matches not accepted after 1 day
$arrayin = array();
$autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " "
. "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < " . ((int) time()) . ")");
if (mysql_num_rows($autorefund) > 0) {
while ($autorefund = mysql_fetch_assoc($autorefund)) {
$arrayin[] = $autorefund['id'];
mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " "
. "WHERE `id` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ")");
mysql_query("DELETE FROM `challenges` WHERE `a` IN "
. "(" . mysql_real_escape_string(implode(',', $arrayin)) . ") "
. "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 "
. "AND (`expires` < " . ((int) time()) . ")");
}
}

最佳答案

你需要发布 MySQL 更新,删除循环之外的条件。

您已经在循环中获取 ID。

所以,最终的代码应该是:

 //Delete all matches not accepted after 1 day
$arrayin = array();
$autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")");
if (mysql_num_rows($autorefund) > 0) {
while ($autorefund = mysql_fetch_assoc($autorefund)) {
$arrayin[] = $autorefund['id'];
}
}
mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " WHERE `id` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ")");
mysql_query("DELETE FROM `challenges` WHERE `a` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ") AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")");

不要使用 mysql_ 函数,因为它们已被弃用,并将在未来的 PHP 版本中删除。

关于php - 使用 id 数组更新和删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com