简体   繁体   中英

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. I want to make it so the user can only update the value every 3 days. 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. 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

//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. 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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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