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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Double insert into mysql db

    hi
    I have a problem with insert record into mysql db.
    When i save a record into db, this is saved two times. This is not correct!!

    Code:
    <?
    
    if (!empty($_GET['IDA']))
     {
     $indice=$_GET['IDA'];
     }
      else
     {
      $indice="";
     }
     
    if ($indice=="")
    {
    $attivita= new attivita("Nuovo",$indice1);
    }
    else
    {
    $attivita= new attivita($indice,$indice1);
    }
    
    
    $attivita->tipologia=$_POST['tipologia'];
    $attivita->nota=$_POST['note'];
    
    $attivita->Data=convertiData($_POST['date1xx']);
    $attivita->Ora =$_POST['ora'].":".$_POST['minuti'];
    $attivita->scadenza=$_POST['idsca'];
    $idattivita=$attivita->salva_attivita($indice);
    
       echo "<script language='Javascript'>top.visibile.location.href='$indirizzo';</script>";
    
     ?>
    Code:
    function salva_attivita() {
    		$connessione = & connetti_database();
            
      
             mysql_query("LOCK TABLE Attivitą WRITE") or die("Query non valida: " . mysql_error());
    
             if ($this -> id == "Nuovo") {
                $query = "REPLACE INTO Attivitą(pratica, scadenza, Tipologiattivita, Note, idutentecreatore, datacreazione, idutentemodifica, dataultimamodifica, Data, Ora) VALUES (" . $this->pratica . ", NULL, " . $this->tipologia . ", '" . $this->nota . "'," . $this->idutentecreatore . ",'". $this->datacreazione ."',". $this->idutentemodifica .",'" . $this->dataultimamodifica."','". $this->Data ."','".$this->Ora."')";
    			
    mysql_query($query) or die("Query non valida inserimento attivitą: " . mysql_error());
    
    $query2="SELECT * FROM Attivitą WHERE ID=LAST_INSERT_ID();";
    $row = mysql_fetch_row(mysql_query($query2));
    $id_insert = $row[0];
    			
             } else { 
    $query = "UPDATE Attivitą SET  note= '" . $this->nota . "', Tipologiattivita= " . $this->tipologia . ", idutentemodifica=" . $this->idutentemodifica . ", dataultimamodifica='". $this->dataultimamodifica . "', Data='". $this->Data."', Ora='". $this->Ora . "', Scadenza=NULL WHERE id=" . $this->id;
    
    mysql_query($query) or die("Query aggiornamento attivitą non valida: " . mysql_error());
    			
    	        
                    mysql_query("UNLOCK TABLES");
    
             mysql_close($connessione);
             
    return $id_insert;
           
             
    	}
    I am 100% sure (and I really mean 100%) that my script is ok.
    I don't execute the query twice or my script isn't run twice.

    Help me please

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The quickest way to figure out which two queries are getting executed would be to print them to the screen. Every time you run the mysql_query command echo out that query to the page. When you run the script you will be able to see which two are running and then go back and figure out why.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    QUESTION? are you calling a javascript code block to make this happen? if so stop banging your head on the php/mysql code i just found out that javascript was calling the page twice which in turn inserted the data twice. i used a prechecker and the second run caused the pre checker to stop the second insert. now i need to figure out how to kill the second call from the javascript.

    when you get a chance you may want to view this thread .. Double insert in MySQL

  • #4
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Double Insert problem

    I have the same problem with double insertion.

    As atapoverseer mentioned, even though i tried printing at each step, it still inserts twice, although its actually printing the lines once, and not twice. but the records are inserted twice.

    Any more ideas?

    I am not using JS or forms of any kinds. its a simple serial script...

    Quote Originally Posted by Spookster View Post
    The quickest way to figure out which two queries are getting executed would be to print them to the screen. Every time you run the mysql_query command echo out that query to the page. When you run the script you will be able to see which two are running and then go back and figure out why.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,080
    Thanks
    2
    Thanked 321 Times in 313 Posts
    Browsers will request a page twice for several reason - FF when it applies the visitor's character encoding choice, IE when it requests your favicon.ico file, all browsers if you have a URL rewrite rule that is either missing or has a trailing slash on a folder (I don't remember which)...

    There are two common ways to detect and prevent the double database insert -

    1) If something about the data is unique so that one of the values will only exist once in the database, make that column a unique key so that the second insert will produce a duplicate key error and won't insert the second time, or

    2) Use a session variable to detect that the form processing code has already been executed once. Set some session variable when the query executes and test if this variable is set at the start of the form processing code. Skip the form processing code if the variable is already set.

    The reason printing the query does not show you anything in this case is because the page is requested/refreshed and unless you are very fast you won't see the two copies of the page being displayed.
    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.


  •  

    Posting Permissions

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