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

Php 如何限制用户将mysql值更新到数据库的频率,php,mysql,database,Php,Mysql,Database,我的网站上有一个字段,用于更新mysql数据库中的值。我想让用户每3天只能更新一次值。我该怎么做呢 以下是我到目前为止编写的代码: <?php if(isset($_POST['Update'])){ $UpdateHWID = $_POST['HWID']; $sql = $con->query("UPDATE users SET HWID = '{$UpdateHWID}' where UserID = $User");

我的网站上有一个字段,用于更新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中创建新行(上次更新)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 
}
//返回sql上次更新(选择db…)
$current_Data=日期('Y-m-d');
$current\u Data\u time=strottime($current\u Data)//将数据转换为时间
$last_update_p3=strotime(“+3day”,strotime($last_update));
$last\u update\u p3=strotime($last\u update\u p3)//将数据转换为时间
if($current_Data_time query(“updateusers SET HWID='{$UpdateHWID}',last_UPDATE='{$current_Data}',其中UserID=$User”);
//用当前日期更新上次数据
}
其他的
{
//还不到三天
}
根据Pinx0,如果向用户表中添加包含上次更新日期的新列,则可以创建一个条件。例如:

ALTER TABLE `users` 
ADD `lastUpdated` DATE NOT NULL;
现在,您可以向现有查询添加如下条件:

UPDATE `users` 
SET `HWID` = '{$UpdateHWID}',
    `lastUpdated` = NOW()
WHERE `UserID` = $User AND 2 < DATEDIFF(CURDATE(),`lastUpdated`);
更新“用户”
设置'HWID`='{$UpdateHWID}',
`lastUpdated`=NOW()
其中,`UserID`=$User和2
使用DATEDIFF方法,您可以很容易地做到这一点

这将是我的第一个评论。所以,如果我写过,请告诉我 这里有点不对劲


这是否意味着用户可以更新一次,然后他/她可以在3天后更新?同时他/她不能?这意味着用户可以更新一次,然后他/她不能再更新3天。
$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
}