Rubid
Rubid

Reputation: 59

How to run a code after a specified time?

I have a big problem:

A user logs in, and the session opens. After that he clicks a button (in a form) which action is "example.php". This example.php should increase one record in MySQL database by 1.

How to write this "example.php"? Please help.

EDIT: Sorry, I haven't asked what i was supposed to ask.

Main problem:

After a user clicks a button, the example.php script should execute after a specified time, for example, a 600 sec.

The other problem is that the user can click the button and log out, and despite that, the example.php should execute 600 sec later.

EDIT (18:48): OK, I've read all your suggestions, but dont't have an idea how to make all of this things work together.

I made a form.php, with input type="submit" and action="example.php". I want to: 1. start a javascript timer; 2. increase a value in database after a time, specified earlier in a variable.

Please, if it is possible, give me an example, how to do this.

Upvotes: 4

Views: 15236

Answers (4)

m4rc
m4rc

Reputation: 3006

There are several ways you could go about doing this... As mentioned there is the sleep method. You could have a job - i.e. Add something to a jobs list and have a cronjob check it every so often to see if it is due. You could use a javascript timer to execute after x number of seconds.

Upvotes: 0

hsz
hsz

Reputation: 152216

In example.php execute SQL:

UPDATE table SET field = field + 1;

Do you need more info to do that ?

EDIT:

I think that there is no other way like jobs mechanism. When user calls example.php, you add to the database new job with a current timestamp + 600 seconds.

Parallel there should be running some job executor that will gather from database all jobs that have timestamp set to timestamp <= NOW().

If it will some records, call specified piece of code and remove/mark as done that jobs.

Upvotes: 2

ANisus
ANisus

Reputation: 77965

<?php
if( isset($_POST['submit_button']) ) {

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

    $sql = 'UPDATE table SET counter_column = counter_column + 1'; // WHERE table_name_id =' . intval($id);
    mysql_query($sql, $link);
}
?>

This example.php will only update the value if the submit_button is clicked on and not if you just type in the URL example.php

Upvotes: -1

Thariama
Thariama

Reputation: 50832

You may delay the execution of code using sleep

Example:

<?php
    echo 'Script Start: '.date('h:i:s') . '<br>';
    sleep(5);  // delay in seconds (here 5)
    echo 'Script Ende: '.date('h:i:s') . '<br>';
?>

I do not know if what you are trying to do makes perfect sense, but your exampl.php would look something like:

<?php
    sleep(600);  // delay in seconds
    // the code to be executed delayed here
?>

Upvotes: 0

Related Questions