Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Why is this code inserting duplicate entries?

    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 :

    PHP Code:
    mysql_connect($server_ip,$username,$password);
                        @
    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');
                        
    mysql_close(); 
    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!
    -DR.Wong

    Wheres the food at?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,010
    Thanks
    2
    Thanked 312 Times in 304 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.

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #4
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Hey!

    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.
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •