【发布时间】:2019-06-27 03:52:49
【问题描述】:
我试图让确认码在 mysql 表“winners”上的获胜日期过早 1 天时过期。我的 php 代码工作正常并将 Confirmation_status 设置为 "expired" ,但是当有超过 1 行具有相同的 id_recharge_winner 时,脚本会将所有这些行设置为 "expired" 。我希望只有年长的被驱逐。
请帮帮我
我的赌桌赢家是这样的:
|id_recharge_winner |confirmation_code |confirmation_status| date_confirmation |date_win|
-------------------------------------------------------------------------------------------
1 |8eomdv | not confirmed |NULL |2019-01-20 23:58:41
1 |ioozpu | not confirmed |NULL |2019-02-02 09:57:10
1 |cpq2vp | not confirmed |NULL |2019-01-26 01:05:18
2 |tnymsp | not confirmed |NULL |2019-02-02 01:09:54
2 |qh8lqq | not confirmed |NULL |2019-02-02 06:14:37
2 |jgg3xi | not confirmed |NULL |2019-01-26 01:22:40
3 |cukxc5 | expired |NULL |2019-01-26 01:33:11
4 |3ixoj4 | not confirmed |NULL |2019-01-26 01:43:42
5 |20bqrn | not confirmed |NULL |2019-01-26 11:18:16
6 |lebx61 | not confirmed |NULL |2019-02-02 12:40:27
6 |7tgoaz | not confirmed |NULL |2019-01-26 12:42:41
6 |kphs5k | not confirmed |NULL |2019-01-26 12:51:33
6 |6vxcy9 | not confirmed |NULL |2019-01-26 13:07:23
7 |sttyul | not confirmed |NULL |2019-01-26 13:11:47
我的 php:
for ($i=1;$i<=7;$i++){
//Verify if confirmation code is expired
$sql_expired = "SELECT id_recharge_winner, date_win, date_confirmation
FROM winners
WHERE id_recharge_winner ='$i'";
$result_expired = mysqli_query($conn, $sql_expired);
if (mysqli_num_rows($result_expired) > 0) {
while($row = mysqli_fetch_assoc($result_expired)){
$id_recharge_winner = $row['id_recharge_winner'];
$date_win = $row['date_win'];
$date_confirmation = $row['date_confirmation'];
$expiration_delay = 1; //One day
if ((time() - $date_win) >= $expiration_delay) {
$sql_set_expired = "UPDATE `winners`
SET `confirmation_status` = 'expired'
WHERE confirmation_status = 'not confirmed'
AND id_recharge_winner = '$id_recharge_winner'";
$set_expired_result = mysqli_query($conn, $sql_set_expired);
}
}
}
}
【问题讨论】:
-
尝试打印
(time() - $date_win)来调试if语句 -
您的第二条 SQL 语句在选择行时没有使用 date_win。您可以添加它,或者直接在 SQL 中作为另一个 where 子句进行 if/time 比较。
-
您的表缺少可用作唯一标识符的主键。通常它会被称为“id”。然后,您可以使用它来确保您的 UPDATE 只会更改您需要的一行。或者,您可以在更新查询中插入确认代码以缩小结果范围,但这不是正确的做法。
-
我不知道该怎么做,你能把它放在这里吗?
-
不要存储指示是否过期的字段。只需使用到期日期即可。而且,请务必在需要时检查该日期。
标签: php mysql sql time duration