Nescafe
Nescafe

Reputation: 63

php insert data after a period of time

Currently I have an application whereby when the user click the submit button, it will trigger the insert code to insert the record for reports function.

The problem I'm facing now is, the user not a computer savvy, therefore I've an inaccurate data from the table. Below are the few examples that cause the data capture incorrectly.

Case 1: Once the user click the submit button, the data already insert to the database but the user accidentally hits the submit button again and the insert trigger again and causing the database has two same records with a different timestamp in about 5 to 10sec.

Case 2: The first record user enter is actually not the user wants, for example, the user wants to key-in 29/10/2013 but the user key-in as 28/10/2013 and the user again hit the back button and insert the correct one which is 29/10/2013 and a single transaction it have 2 record which is one is the correct date and one is the mistake date.

Below are the code:

$stmt = $db->prepare("INSERT INTO log (dob, log_datetime, log_count, amount) VALUES (?, ?, ?, ?)");
$stmt->bindParam(1, $userDate);
$stmt->bindParam(2, $now, PDO::PARAM_STR);
$stmt->bindParam(3, $cust_count);
$stmt->bindParam(4, $amount);

date_default_timezone_set('Asia/Kuala_Lumpur');
$now = date('Y-m-d H:i:s');
$cust_count = 1;
$amount = 10;
$stmt->execute();

The code above trigger whenever the user hits the submit button, so I was thinking is there possible to make the insert code to trigger after a period of time, example 30sec?

Additional Notes

  1. This is an application developed and execute the database from the table
  2. It has nothing to do with form validation, as I already coded the form validation part.
  3. My question is is there any way to trigger the insert query on specific amount of time set

I search in SO and Google, but I couldn't find a relevant example to work with.

Please suggest me any method that can fix the above two case scenarios.

Thank you.

Upvotes: 0

Views: 1081

Answers (5)

kuroi neko
kuroi neko

Reputation: 8651

Trying to catch all UI leaks can prove difficult and cumbersome.

I would rather use the timestamp in the DB as a temporal filter

I would query the most recent records (in the last 30 seconds) using cust_count as a candidate key, and update the last record (if any) instead of creating it anew.

Schematic example:

function update_log ($entry)
{
    $recent = $db->select (
          "id FROM log 
          WHERE timestamp > now()-30 seconds
          AND   user = $entry->user");
    if ($recent)
    {
        // overwrite last record with current entry (and refresh timestamp)
    }
    else
    {
       // create a new entry
    }
}

You could even delete all logs of the user adding an entry that are newer than 30 seconds.

Upvotes: 0

Steve Clay
Steve Clay

Reputation: 8781

Disable the submit button upon submitting, and on the post-submission page, show the user the submitted data and give them a clear way to edit it.

Users generally suspect that hitting back into a form is not a good idea, but you're not giving them any better options.

Upvotes: 0

Jorge Barata
Jorge Barata

Reputation: 2317

MySQL doesn't have that behavior. I think that what you are looking for is an asynchronous task queue, like Celery

Upvotes: 0

falonofthetower
falonofthetower

Reputation: 98

https://www.php.net/sleep

sleep(30);

$stmt->execute();

Will do what you want I believe.

Upvotes: 0

Jorge Barata
Jorge Barata

Reputation: 2317

For the first case, disable the button when the form is submited. For the second case, validate the date before inserting it into the database.

Upvotes: 1

Related Questions