Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
03-01-2009, 08:13 PM #1
Why is this code inserting duplicate entries?
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');
Anyone have a solution/way of checking exactly what's going on here?
Thanks a lot!-DR.Wong
Wheres the food at?
03-01-2009, 08:51 PM #2
- Join Date
- Oct 2006
- Denver, Colorado USA
- Thanked 326 Times in 318 Posts
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.
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
03-02-2009, 03:45 AM #3
- Join Date
- Feb 2009
- Thanked 1 Time in 1 Post
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, 05:41 AM #4
Thanks for all of the replies.
I didn't give enough info I guess.
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.
Wheres the food at?