用户24小时按一次按钮?建站

lc8prwob  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(260)
<?php
$username = $_SESSION['username'];  
include('dbcon.php');   
if (isset($_POST['req'])) {

        $query = "SELECT requested FROM users WHERE username='$username' ";
        $result = mysqli_query($db, $query);
        $row = mysqli_fetch_array($result);
        if(empty($row['requested'])) {

    $request = mysqli_real_escape_string($db, $_POST['request']);
    $query = "INSERT INTO requests (username, airid) 
              VALUES('$username', '$request')";

    mysqli_query($db, $query);

    $query2 = "UPDATE users SET requested='1' WHERE username='$username'"; 
    mysqli_query($db, $query2); 
    // timer should go here i guess

    echo '<script>swal({title: "Great!", text: "We will process your request as soon as possible!", type: "success", buttonsStyling: false, heightAuto: false}).then(function(){ location.reload();});</script';    

        } else { 

            echo '<script>swal({title: "Whoa!", text: "You must wait 48 hours before requesting again!", type: "error", buttonsStyling: false, heightAuto: false}).then(function(){ location.reload();});</script'; 
}
}
?>

如何在将users requested=设置为“1”后设置计时器,以便在24小时后,他的请求将不再是“1”,而是“空”
对不起,我的英语太差了

tjvv9vkg

tjvv9vkg1#

考虑以下情况,它使用一分钟而不是一天作为约束的单位:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table 
(id SERIAL PRIMARY KEY
,user_id INT NOT NULL
,dt DATETIME NOT NULL
);

INSERT INTO my_table (user_id,dt) VALUES (101,NOW());
Query OK, 1 row affected (0.00 sec)

SELECT * FROM my_table;
+----+---------+---------------------+
| id | user_id | dt                  |
+----+---------+---------------------+
|  1 |     101 | 2018-10-07 23:21:44 |
+----+---------+---------------------+
1 row in set (0.00 sec)

…大约一分钟后。。。

INSERT INTO my_table (user_id,dt) 
SELECT 101,NOW() 
  FROM (SELECT 1) x 
  LEFT 
  JOIN my_table y 
    ON y.user_id = 101 
   AND y.dt >= NOW() - INTERVAL 60 SECOND 
 WHERE y.id IS NULL;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

SELECT * FROM my_table;
+----+---------+---------------------+
| id | user_id | dt                  |
+----+---------+---------------------+
|  1 |     101 | 2018-10-07 23:21:44 |
|  2 |     101 | 2018-10-07 23:23:39 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

…几秒钟后。。。

INSERT INTO my_table (user_id,dt) 
SELECT 101,NOW() 
  FROM (SELECT 1) x 
  LEFT 
  JOIN my_table y 
    ON y.user_id = 101 
   AND y.dt >= NOW() - INTERVAL 60 SECOND 
 WHERE y.id IS NULL;
Query OK, 0 rows affected (0.00 sec)

SELECT * FROM my_table;
+----+---------+---------------------+
| id | user_id | dt                  |
+----+---------+---------------------+
|  1 |     101 | 2018-10-07 23:21:44 |
|  2 |     101 | 2018-10-07 23:23:39 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

相关问题