avoid double insert(form submit) by accident

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

avoid double insert(form submit) by accident

Post by jmut »

How can I prevent from double insert by accident. For example refreshing...posting data and voala two records there.
I would rather not use header redirect with sessions.
Another ways I know is using some kind of SELECT to check if "such" record exists already...which is not always possible to do :(

Are there any other means to address this problem?

P.S.
I was not sure if here or theory and design is better.
And I couldn't find a topic on this problem.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The first thing you can do is to avoid double clicks on the button by using the onSubmit or onClick event handlers to disabled the submit button. :)

Next is the data checking, normally a simple 'select count(*) where column = input' for every column and input that is or will be updated by the form is enough to show if it is duplicate data.

Or.. you can use a timestamp and a predefined period in which the user must wait before they can submit again, like these boards do. Simply create a timestamp upon submission, and have your code check that timestamp before every submittal.

Code: Select all

<?php

$query = "SELECT `timestamp` FROM `users`WHERE `userid` = '{$userid}' LIMIT 1";
$result = mysql_query($query) or die('DB Error');
$timestamp = (int) mysql_result($result, 0, 'timestamp');

if (($timestamp + 120) > time()) {
    die('You may not post again so soon, please try again in a few moments.');
}

?>
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Are you meaning insertion into databse ? Then, amongst the data which you need to insert, have an unique data which will represent the entire set of data. For example, if your saving user information, user email might be considered as primary key. With this key, you can have other extra keys like phone number, user first name etc etc. Based upon these, you can give a 'check for duplicate' button, which will check the database if already similar data exists. if so, it gives a warning. Otherwise, if gives a 'go' message, after which you can insert the data.

Another addition is, when you 'insert' data, set some $inserted to a value. when you refresh the form set this to another value. Before 'insert' code executes make sure that either the $inserted is 'null' specifying the data is fresh or a value corresponding to reset'ed form. This will ensure that the data is inserted only from a new form or a form which has been reset. This will prevent you from inseting data from the form, in case the form has been accessed by pressing back button in the browser. May be, I am wrong in this idea, but that was just an idea :D
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Thank you Jenk and technofreak :)
I did it with the select. Just before the inserts I make a select statement with pretty much all data coming for the insert just used in WHERE clause for select.
Of course I had to remove some fields I searched for like picture_path, create_date and stuff like that which actually change on each insert and looking for them will always result in 0.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Post Reply