怎样才能让一个确认码到期时老那么1天

问题描述 投票:1回答:2

我试图做一个确认码到期时取胜的日旧再用1一天的MySQL表“赢家”。我的PHP代码工作正常,并设置为“已过期”的confirmation_status,但是当有多于1行具有相同id_recharge_winner,脚本使所有那些“过期”行。我想只是旧的被exoired。

请帮帮我

我的表赢家是这样的:

|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);
            }
        }
    }
}
php mysql sql time duration
2个回答
1
投票

我怀疑,你可以简单地得到与选择/更新所有相关记录单查询完成工作。

试想一下:

UPDATE winners w
SET w.confirmation_status = 'expired' 
WHERE 
    w.id_recharge_winner = ?
    AND w.confirmation_status = 'not confirmed' 
    AND DATEDIFF(CURDATE(), w.date_win) > 1
    AND NOT EXISTS (
        SELECT 1
        FROM winners w1
        WHERE
            w1.id_recharge_winner = w.id_recharge_winner
            AND w1.confirmation_status = w.confirmation_status
            AND w1.date_win < w.date_win
    )

WHERE条款适用相关的过滤器;如果没有记录匹配,没有UPDATE发生。该条款还incudes一个NOT EXITS条件,即使用相关,以确保该记录被更新(如果有的话)是最古老的一个(即不存在旧记录)。

PS:我取代PHP变量$id_recharge_winner与绑定参数(?);你应该看看how to use parameterized queries让你的代码更安全和更清洁。


0
投票

timestampdiff子句中使用WHERE只能选择行那里获得了现在的时间之间的差大于或等于一天。你不需要更新表,可能不应该。每当你想过期胜使用类似于下面的东西。

SELECT ...
       FROM winners
       WHERE ...
             AND timestampdiff(day, date_win, now()) >= 1
             ...
       ...;

如果你想在没有过期的,否定的条件。

SELECT ...
       FROM winners
       WHERE ...
             AND timestampdiff(day, date_win, now()) < 1
             ...
       ...;
© www.soinside.com 2019 - 2024. All rights reserved.