【问题标题】:How can make a confirmation code expires when old then 1 day如何让确认码在 1 天后过期
【发布时间】: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


【解决方案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。该子句还包含一个NOT EXITS 条件,它使用相关来确保正在更新的记录(如果有)是最旧的记录(即没有较旧的记录)。

PS:我用绑定参数 (?) 替换了 PHP 变量 $id_recharge_winner;您应该查看how to use parameterized queries 以使您的代码更安全、更干净。

【讨论】:

    【解决方案2】:

    在您的WHERE 子句中使用timestampdiff 以仅选择赢得时间与现在之间的差异大于或等于一天的行。您不需要更新表,可能也不应该。每当您想要过期的胜利时,请使用以下内容。

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

    如果你想要未过期的,否定条件。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多