简体   繁体   English

我如何使用PHP更新mySQL,但前提是最近一次更新已超过24小时?

[英]How do I update mySQL with PHP, but only if the last update was more than 24hrs ago?

I have a table of different users, but I want to be able to limit how frequently the user can update their own information. 我有一个不同用户的表,但我希望能够限制用户更新自己信息的频率。 In the table, the timestamp is automatically updated when the user makes a change. 在表中,当用户进行更改时,时间戳会自动更新。

The page automatically updates their information with UPDATE edits SET EDITS=EDITS+1 WHERE ID='$sID' when it loads, but I would like for that to be ignored if the last update was less than 24hrs ago. 该页面在加载时会自动使用UPDATE edits SET EDITS=EDITS+1 WHERE ID='$sID'更新其信息,但是如果最近一次更新少于24小时,我希望忽略该信息。

I'm not sure if that can be solved in UPDATE, or if there is a better alternative. 我不确定是否可以在UPDATE中解决,或者是否有更好的选择。

Thanks! 谢谢!

假设您最近更新的last_updated

UPDATE edits SET EDITS=EDITS+1 WHERE ID='$sID' AND DATE_SUB(NOW(), INTERVAL 24 HOUR)>last_updated

You will need to add a column to the database containing the last update. 您将需要在数据库中添加一列,其中包含最近的更新。 Let's assume it's called last_update and DATETIME format. 假设它称为last_update和DATETIME格式。

$one_day_ago = strtotime('24 hours ago');

Then run this query: 然后运行以下查询:

UPDATE edits SET EDITS=EDITS+1 WHERE ID='$sID' AND last_update <= FROM_UNIXTIME('$one_day_ago')

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

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