For no good reason this code sometimes inserts the data twice into my mysql database. It seems to happen only from IE and XP and it seems to only happen randomly. Is it a POST or GET thing from the referring page? Any help would be great.
Thanks,
-Sky.
Here is my code:
PHP Code:
<?php
$connection=mysql_pconnect("localhost","user","password"); if (!$connection) { echo "Could not connect to MySQL server!"; exit; }
$db=mysql_select_db("database",$connection); if (!$db) { echo "Could not change into the database mondavi"; exit; }
I think it's only a problem when a visitor double click the submit button, that's all !!
maybe you could have an anti-flood script to avoid this, another thing is to have a unique field in MySQL so the same order_id can't be inserted twice ..
__________________ PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
I have an auto increment 'id' field in my MySQL table, which is unique for each item ordered. At the moment it just moves on to the next value when it gets the double entry. How can I use it to allow only one insert to the database?
About the double submit, it's kinda hard to get away from because I use select lists to update the page and they submit the form when they update.....would moving the insert code into the same page and putting in a function help?
The purpose of auto_increment (at least the major part of it) is to allow inserting more than one records without worrying about duplicate (primary key) entries. So you don't want to try to limit that. If there is anything else that is unique across the data (e.g. SIN) then you can set this as primary key.
I am not sure what your form looks like but one thing I do sometimes is to refresh the page right after I do all the input check/processing (e.g. inserting into database). "Refreshing" I mean here is to use header('Location: '.$_SERVER['PHP_SELF'].'?action=thanks'); or something (could be REQUEST_URI instead of PHP_SELF, depending on what you are doing).
I can then place a confirmation handling message if ($_GET['action'] == 'thanks'). But this doesn't help sometimes, like when the user enters very sensitive info and you don't want to carry around in GET or SESSION.