简体   繁体   English

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

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

I have a field on my website, which updates a value in my mysql database.我的网站上有一个字段,它更新了我的 mysql 数据库中的一个值。 I want to make it so the user can only update the value every 3 days.我想这样做,以便用户只能每 3 天更新一次该值。 How would I go about doing this?我该怎么做呢?

Here is the code that I have written so far:这是我到目前为止编写的代码:

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

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

          echo "HWID Updated Successfully!";
        }
?>

Use a last updated field in mysql (date and time of last update), and check it before making the update.使用 mysql 中的最后更新字段(最后更新的日期和时间),并在进行更新之前检查它。 If satisfies your condition then commit the update and also update that time field, if not show error to the user.如果满足您的条件,则提交更新并更新该时间字段,如果不满足则向用户显示错误。

Create new row into db (last_update) type= data在 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 
}

According to Pinx0 , if you add a new column to your users table which contains the date of the last update, then you can create a condition.根据Pinx0 ,如果您向包含上次更新日期的用户表添加一个新列,那么您可以创建一个条件。 For example:例如:

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

Now you can add a condition to your existing query something like this:现在您可以向现有查询添加条件,如下所示:

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

You can easily do that, using the DATEDIFF method.您可以使用 DATEDIFF 方法轻松地做到这一点。

It will be my first comment.这将是我的第一条评论。 so, Let me know if I have written something incorrectly here.所以,如果我在这里写错了什么,请告诉我。

$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