double submitting

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
roice
Forum Commoner
Posts: 35
Joined: Tue Mar 02, 2010 9:14 am

double submitting

Post by roice »

Hello,

From time to time I found duplicated records (rows) in my database. Its looks like the user double click on the submit button.
recently its happen more often so I add code that check when the last submit happened (I save the linux time) and check if there is 10 second delay:

Code: Select all

	$query = mysql_query("SELECT create_date FROM `tkts_topics` WHERE (client = '$client') AND (email = '$email') AND (title = '$title') AND ($date - create_date <= 10) LIMIT 0,1");
	if (mysql_num_rows($query))
	{

}
I don't know why but its not working.
is there any better way to prevent double submiting?

Roi.
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: double submitting

Post by Tiancris »

If you are suspecting a double sending data, you could try disabling the button when the user clicks.

Code: Select all

<form>
<input type="button" onclick="buttonClick(this)">
</form>
<script type="text/javascript">
function buttonClick(btn)
{
  btn.disabled = true;
  btn.form.submit();
}
</script>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: double submitting

Post by requinix »

Server-side there are a couple common options:
- Check for recent duplicate data. Certainly easier to do but it might not always be possible.
- Generate a random ID, stick that in the session and hide it in the form, and when the form is submitted check that the ID hasn't been "used" yet. That could be the fact that it's still stored in the session: if it's set then the ID is valid, you do the work, and unset it, and if it's not set then you know the form was submitted twice.
headbull
Forum Newbie
Posts: 10
Joined: Thu Jun 07, 2012 7:18 am

Re: double submitting

Post by headbull »

What can happen is also when you submit forms, and the user refreshes the "loading" page he submits the data again. A solution to this can be to redirect the user with header(); to a page that confirms the submission (submitconfirm.php?submitid=XXX <- if you need the data reviewed/confirmed without the risk of submitting it again).
Post Reply