簡體   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