View Full Version : Why is this code inserting duplicate entries?

03-01-2009, 09:13 PM
Hey everyone,

I use hundreds of MySQL INSERT queries all over my website.

Suddenly this one is inserting into the database twice.

IMPORTANT : The first entry has values that are posted from the form page, the second entry only contains info generated on the processing page (for example the date or session user)

Here is the code :

@mysql_select_db($database) or die( "Unable to select day end database");
$query_de = "INSERT INTO day_end VALUES ('','$system_sales','$c1','$c2','$c5','$c10','$c20','$c50','$r1','$r2','$r5','$r10','$r20','$r50','$ r100','$r200','$total','$float','$manager','$date','$time','1','','')";
mysql_query($query_de) or die('Problem inserting');

I have been struggling with this for hours now.

Anyone have a solution/way of checking exactly what's going on here?

Thanks a lot! :thumbsup:

03-01-2009, 09:51 PM
Your browser is making a second request to the url of your page. Your code is also not checking if the form was submitted and it is not validating the data.

You cannot control how the various browsers operate, but here are some of the reasons why a page gets requested twice - FF will request a page a second time to apply the default character encoding that the visitor has set, IE will request a page a second time when it fetches the favicon.ico, some url_rewriting will cause a browser to request the page a second time, and you might be doing some redirecting in your code that could cause this.

Your form processing code must check that the submit button on the form has been set and you should be validating all the data to insure it contains expected values. If the form submit is not set or the data does not exist, you should not be executing the query.

03-02-2009, 04:45 AM
You can perform the validation of the form data using the javascript.
After getting the data properly validated, pass the values to the php for insertion in to the database.
During insertion too, you can check whether the data to be inserted already exists in the database table or not.

And, as you said, the same data is getting inserted twice..so you can keep a check whether the current inserted data is same as the last inserted data.if yes, then break; else continue with the insertion.

03-02-2009, 06:41 AM

Thanks for all of the replies.

I didn't give enough info I guess.

The script consists of two pages, the first contains the form, where validation takes place via javascript, and the second is a completely separate page where the posted variables from the 1st page as well as a few variables generated in the second page are inserted into the database.

The same data is not being inserted twice.

The first entry is correct. It contains both info from the 1st page as well as info from the second page, such as the current date : date('d-y-m').

The second entry that is appearing only contains info generated on the 2nd page (ie the date).

There is also a time variable that is generated on the second page and both of the entries have the same time variable down to the second.

The only obvious answer is that the SECOND page is being submitted a second time for some reason, but this is not occuring as some result from the 1st page as the post variables are blank.

I will try isset and see what happens.

Thanks again for the responses.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum