繁体   English   中英

如何限制用户将 mysql 值更新到数据库的频率

[英]How to limit how often a user can update a mysql value to a database

我的网站上有一个字段,它更新了我的 mysql 数据库中的一个值。 我想这样做,以便用户只能每 3 天更新一次该值。 我该怎么做呢?

这是我到目前为止编写的代码:

<?php
        if(isset($_POST['Update'])){
          $UpdateHWID = $_POST['HWID'];

          $sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' where UserID = $User");

          echo "HWID Updated Successfully!";
        }
?>

使用 mysql 中的最后更新字段(最后更新的日期和时间),并在进行更新之前检查它。 如果满足您的条件,则提交更新并更新该时间字段,如果不满足则向用户显示错误。

在 db (last_update) type= data 中创建新行

//return to sql last_update (select db ...)

$current_Data = date ('Y-m-d');
$current_Data_time = strtotime ($current_Data); //convert data to time
$last_update_p3 = strtotime ("+3day", strtotime($last_update));
$last_update_p3 = strtotime ($last_update_p3);//convert data to time

if($current_Data_time <=$last_update_p3)
{
 $sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' , last_update='{$current_Data}'  where UserID = $User");
//update last data with current date 
}
else
{
//It has not gone three days 
}

根据Pinx0 ,如果您向包含上次更新日期的用户表添加一个新列,那么您可以创建一个条件。 例如:

ALTER TABLE `users` 
ADD `lastUpdated` DATE NOT NULL;

现在您可以向现有查询添加条件,如下所示:

UPDATE `users` 
SET `HWID` = '{$UpdateHWID}',
    `lastUpdated` = NOW()
WHERE `UserID` = $User AND 2 < DATEDIFF(CURDATE(),`lastUpdated`);

您可以使用 DATEDIFF 方法轻松地做到这一点。

这将是我的第一条评论。 所以,如果我在这里写错了什么,请告诉我。

$currentDate = date('Y-m-d');

$query = "SELECT DATEDIFF($currentDate,*Last_update_date_row_name*) as diff FROM *TABLE_NAME* WHERE user_id=Current_User_Id";

$result = mysqli_query($conn,$query);

if($result!=false){
  //compare the diff with your desire number
  // then you can hide or disable the button or show error
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM