如何使用 PHP 插入延迟 sql 语句?

发布于 2024-11-16 06:20:55 字数 531 浏览 4 评论 0原文

我想为用户提供一个网站,当用户输入一些内容时,我会将其放入数据库中。但当用户访问 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

说好的呢 2024-11-23 06:20:55
  • 当用户输入时,将该输入保存在数据库表中,但设置列(已验证)值“0”
  • 一小时后 cron job 会将列(已验证)值设置为“1”
  • 如果用户在验证之前取消,请删除该记录

示例:

user_input

user_id | input |    input_time     | verified
   1    |  500  | 2011-01-01 12:20  |    1
   2    |  700  | 2011-01-01 01:20  |    0

  • 创建一个新的存储输入请求的表
  • 一小时后cron 作业会将所有请求传输到原始表。
  • 如果用户在一小时之前取消,请从请求表中删除该记录。

示例:

user_input

user_id | input
   1    |  500 
   2    |  700 

user_requests

user_id  |  input  |    input_time
   3     |   200   | 2011-01-01 07:20   
  • When user input, save that input in database table but set a column(verified) value '0'
  • After one hour a cron job will set column(verified) value as '1'
  • If user cancel before verification, delete that record

Example:

table user_input:

user_id | input |    input_time     | verified
   1    |  500  | 2011-01-01 12:20  |    1
   2    |  700  | 2011-01-01 01:20  |    0

OR

  • create a new table to store input requests
  • After one hour a cron job will transer all request to original table.
  • If user cancel before one hour, delete that record from requests table.

Example:

table user_input:

user_id | input
   1    |  500 
   2    |  700 

table user_requests:

user_id  |  input  |    input_time
   3     |   200   | 2011-01-01 07:20   
小傻瓜 2024-11-23 06:20:55

cron 对此来说过于复杂。当您插入数据库时​​,有一列包含帖子应该上线的日期和时间。例如:

$query = "INSERT INTO table (body, effective_date) VALUES ('body text', '" . strtotime('+1 hour') . "'";

在显示内容的页面中,只需添加 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:

$query = "INSERT INTO table (body, effective_date) VALUES ('body text', '" . strtotime('+1 hour') . "'";

In the pages that display your content, just add WHERE effective_date>=NOW() and it'll exclude pending entries.

世俗缘 2024-11-23 06:20:55

以下是完成此操作的工作流程:

  1. 当用户发出请求时,将请求记录到文件中
  2. 创建一个 cron 作业来运行单独的 PHP 脚本,该脚本读取文件并将数据插入数据库
  3. 如果用户取消请求,则删除来自文件的请求。

在“文件”的位置,您还可以将请求记录到数据库的“待处理”表中,并执行类似的操作。

Here is a workflow to accomplish this:

  1. When the user makes a request, log the request to a file
  2. Create a cron job to run a separate PHP script which reads the file and inserts the data into a database
  3. If the user cancels the request, remove the request from the file.

In the place of "file" you may also log the request to the database in a "pending" table, and perform a similar operation.

柠檬色的秋千 2024-11-23 06:20:55

有一个不同的数据库表来安排作业。用户可以在此表中添加、修改和删除作业。然后让 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.

娜些时光,永不杰束 2024-11-23 06:20:55

如果您确实希望记录在一小时后输入数据库,您必须为您的条目创建某种队列并设置一个脚本(通过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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文