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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post

    Double insert in MySQL

    I am suffering a tremendously confusing problem. When I call my form handling function (PHP), which inserts a row into a table, the row get's inserted twice ! I am 100% sure (and I really mean 100%) that my script is ok.

    The application I am talking about is running on a default WAMP5 installation.

    My question is simple, has any1 else seen or heard of this same problem ?

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    This happened to me a while ago on a log script i was writing but after me getting very annoyed with it and various changes to my script to try to sort it there was still no change but eventually it sorted itself out (eventually being multiple months in my case) so cant really help you with a solution but i would also be interested to any peoples ideas as to what could have caused it!
    Mike

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It's either the query (syntax), you execute the query twice or your script is run twice.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fix:

    - create a unique index on one of the columns you're inserting values for (or on a combination of columns )
    - use "REPLACE INTO" instead of "INSERT INTO"
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #5
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well I could think of some work arounds, but I was curious if people recognise the error and possibly know of a solution. The unique index is probably the way to go in my case, since I need a workaround if it cannot be fixed...

    Quote Originally Posted by marek_mar
    It's either the query (syntax), you execute the query twice or your script is run twice.
    I am 100% sure neither of these 2 is the case. I tested the same script on a Fedora box with apache and mysql (little older version tho) and I didn't get any errors there.. Next to this I tried stuff like die()-ing the function right after the insert, but even then it inserted the row twice !
    Last edited by 4xz; 03-16-2005 at 11:29 PM.

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I could think of some work arounds, but I was curious if people recognise the error and possibly know of a solution. The unique index is probably the way to go in my case, since I need a workaround if it cannot be fixed...
    Hmm. Something tells me, you're not realy into defencive programming...
    If the rows need to be unique on some field or combination of fields, then the unique index should allready be in place, so that data-integrity is also guaranteed at db-level. Usig the REPLACE is then a nice addition, because for 'legal' inserts, there will be no overhead.

    So it's not realy a workarround, but it's probably the best way to handle inserts on table that can not contain duplicates.

    I am 100% sure neither of these 2 is the case. I tested the same script on a Fedora box with apache and mysql (little older version tho) and I didn't get any errors there.. Next to this I tried stuff like die()-ing the function right after the insert, but even then it inserted the row twice !
    The best way to stay convinced that it's not your code, is not showing your code.
    Maybe add a column and insert microtime() as one of the values...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #7
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    Raf, marek_mar ... you are both right. Was blind blind blind (grrr) to fall for the my-code-is-right-illusion ...

    It turned out to be my code afterall. Not the PHP code but the javascript on the formpage I am pretty embarrassed I was staring dead on the surrounding PHP code, never even came up in my mind that a form can simulaniously be posted to a handlingscript multiple times, the way it did in this case

    Thx for your input anyway

    For the ones interested (and the puzzle-lovers) here's the code that caused this :


    The javascript function :
    Code:
    function submitForm() {
    	if(document.getElementById('NAAM').value == '') {
    		alert('Naam mag niet leeg zijn');
    		document.getElementById('NAAM').focus();
    		return false;
    	}
    
    	var allesOk = datumControle(document.getElementById('BUDGETVAN').value,1);	
    	if(allesOk==true) {
    		allesOk = datumControle(document.getElementById('BUDGETTOT').value,1);	
    		if(allesOk==true) {
    			document.getElementById('formulieraddwerknemer').submit();
    		} else {
    			alert ("Periode tot moet geldige datum bevatten, in formaat 'dd-mm-yyyy'");
    			document.getElementById('BUDGETTOT').focus();		
    			return false;
    		}
    	} else {
    		alert ("Periode van moet geldige datum bevatten, in formaat 'dd-mm-yyyy'");
    		document.getElementById('BUDGETVAN').focus();		
    		return false;
    	}		
    }
    ,the form tag :
    Code:
    <form id="formulieraddwerknemer" action="muteerwerknemers.php" method="POST">
    and the submit button on the form :

    Code:
    <input type="submit" onclick="return submitForm();" value="Opslaan" />
    Last edited by 4xz; 03-17-2005 at 01:16 PM.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I gave 3 possibilitys. This would fall under the "your script is run twice". (so in some way you were right. It was the third option
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well, only thing where I was right, is that my PHP code is 100% good

  • #10
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you find a solution of this problem?
    I have same problem with insert into mysql db!!

  • #11
    New Coder
    Join Date
    Oct 2005
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just had the same problem, banging my head over it...for anyone else who has this problem..CHECK YOUR FORM TAGS

    most likely your PHP is correct, you may have some form tag, and javascript issues

  • #12
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i googled "Double insert in MySQL" to see if i could get my answer too and found this site. i read the thread and added a prechecker at two locations
    and found that the full routine was being call twice by the javascript.

    form starter
    Code:
    <form name=traxit method=post>
    and
    Code:
    <input type=\"submit\" value=\"Send Shipping Email\" onclick=\"gostg4a();\">
    for the submit button.
    Code:
    function gostg4a()
         {
            alert('OK SO FAR');
            
                document.traxit.action = \"code.php\";
    	    document.traxit.target = \"_top\";
    	    document.traxit.submit();
    	    return true;
         }
    so i after running my precheckers it was discovered the next page was called twice and based on what i used to precheck the result was false to the actual action taken so now i have to figure out how to make javascript call the page once and only once!! my 2 cents on this headache!

  • #13
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Many Thanks

    I just had to thank everyone for this posting. I spent WAY too long on what I thought was a PHP/MySQL bug and come to find out it was a lame javascript problem.

    You earned 100 cool points

  • #14
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Double INSERT into MySQL issue resolved for me!

    I was just trying to figure out a solution as I was having it my self. I have other forms that call php functions on another page, but I was not getting duplicate records with them.

    The difference that I found was that my other functions were returning a value to indicate that my function executed as expected.

    With the new function, I didn't have it to return a value. I changed the function to have it to return a value, and I am no longer experiencing the issue. I also write the return value on the page.

  • #15
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    892
    Thanks
    301
    Thanked 2 Times in 2 Posts
    Hi Guys

    Though I know this is a very old post but I would like to add a solution I found to overcome this issue.

    I had the same problem with MyISAM table and then I changed the table type from MYISAM to innodb.

    It solved my duplicate record problem but its still a mystery why was it happening.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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