如何使用 PHP 插入延迟 sql 语句?
我想为用户提供一个网站,当用户输入一些内容时,我会将其放入数据库中。但当用户访问 .php 时,该命令不会执行。我想在一段时间后执行....这是这样的...
10:00 am
User makes the request, and saying that the record will write in the DB one hour later.
11:00 am
The request execute, and the DB write a new record.
我该怎么做?另外,我想为用户添加取消请求的功能......类似这样:
10:00 am
User makes the request, and saying that the record will write in the DB one hour later.
10:30 am
User cancels the request.
11:00 am
Nothing won't execute.
对此有什么想法吗?谢谢。
I would like to provide a web site for user, when the user input somethings, I will put it into the DB. But the command won't execute when the user access the .php. I would like to execute after a while.... This is something like this...
10:00 am
User makes the request, and saying that the record will write in the DB one hour later.
11:00 am
The request execute, and the DB write a new record.
How Can I do so? also, I would like to add an ability for user to cancel the request.....Something like this:
10:00 am
User makes the request, and saying that the record will write in the DB one hour later.
10:30 am
User cancels the request.
11:00 am
Nothing won't execute.
Any ideas on that? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
示例:
表user_input:
或
示例:
表user_input:
表user_requests:
Example:
table user_input:
OR
Example:
table user_input:
table user_requests:
cron 对此来说过于复杂。当您插入数据库时,有一列包含帖子应该上线的日期和时间。例如:
在显示内容的页面中,只需添加
WHERE effective_date>=NOW()
,它将排除待处理的条目。cron is overly complex for this. When you insert into the database, have a column that contains the date and time the post should go live. For example:
In the pages that display your content, just add
WHERE effective_date>=NOW()
and it'll exclude pending entries.以下是完成此操作的工作流程:
在“文件”的位置,您还可以将请求记录到数据库的“待处理”表中,并执行类似的操作。
Here is a workflow to accomplish this:
In the place of "file" you may also log the request to the database in a "pending" table, and perform a similar operation.
有一个不同的数据库表来安排作业。用户可以在此表中添加、修改和删除作业。然后让 cronjob 每五分钟或十分钟检查一次表,并执行当前时间的作业。
Have a different database table where you schedule jobs. Users can add, modify and remove jobs from this table. Then have a cronjob check the table each five or ten minutes, and execute the jobs for the current time.
如果您确实希望记录在一小时后输入数据库,您必须为您的条目创建某种队列并设置一个脚本(通过Cron或其他)检查一小时前的记录并将其插入数据库。您尚未指定您正在使用哪个数据库,但据我所知,没有数据库可以让您选择执行插入命令,但告诉它等待一小时实际上在做。
如果您使用队列,那么您可以在用户创建队列时在其上添加时间戳,并且在该时间戳后的一小时内,用户可以取消记录。
您的队列可能也应该驻留在您的数据库中,并且实际上可能是数据最终要进入的实际表。只需使用您的时间戳来查看一小时是否已经过去,并将记录标记为不再可取消 (如果这个词存在的话......呵呵)。这意味着该条目实际上在一小时过去之前写入数据库。但您只需根据创建后是否已经过了一个小时来过滤您的操作。
If you actually like the record to be entered into the DB one hour later you would have to create some kind of queue for your entries and setup a script (via Cron or other) that checks for records one hour old and inserts them into the database. You haven't specified which database you are working with, but as far as I know there is no DB that gives you the option to do an insert command but tell it to wait one hour before actually doing it.
If you use a queue then you could have a time stamp on it when the user created it and within one hour from that time stamp the user has the ability to cancel the record.
Your queue should probably reside in your DB as well and could actually be the actual table that the data is meant to end up in. Just use you time stamp to see if one hour has elapsed and mark the record as no longer cancelable (if that word exists... hehe). That would mean that the entry is actually written to the DB before one hour has passed. But you just have to filter your operations on whether or not an hour has passed since it's creation.