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
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    INSERT INTO multiple times should be once

    Hi,

    I know I have asked about multiple updates before but this seems to be due to a different issue.

    My INSERT INTO is inserting three times in to my db, and I am not sure why?

    My Code is:
    PHP Code:
    <?php

    include("dbconnect.php");

    function 
    check_input($value){

    if (
    get_magic_quotes_gpc()){
        
    $value=stripslashes($value);
        }
        
    if (!
    is_numeric($value)){
        
    $value mysql_real_escape_string($value);
        }
    return 
    $value;
    }

    foreach (
    $_POST as $key=>$value){
        
    $_POST[$key]= check_input($value);
        }


        
    $sql="UPDATE store SET country ='{$_POST['country']}' 
        WHERE PId={$_SESSION['Id']}"
    ;

    echo 
    $sql '<br />';

        
    mysql_query($sql,$con) or die('Error: ' mysql_error());


        
    $sql2="INSERT INTO purchases (Id, fullname, pphoto, category, subcategory, body, date)
            VALUES
            ('{$_SESSION['Id']}','{$_SESSION['fullname']}','{$_SESSION['pphoto']}','country','{$_POST['country']}',' ordered from ',NOW())"
    ;
    echo 
    $sql2 '<br />';        
        
    mysql_query($sql2,$con) or die('Error: ' mysql_error());

        if(
    $_SESSION['country']=='online purchase'){
            
    $sql3="UPDATE countryepurchases SET {$_POST['ccountry']}=='{$_POST['ccountry']}'
            WHERE PId={$_SESSION['Id']}"
    ;
    echo 
    $sql3 '<br />';

        
    mysql_query($sql3,$con) or die('Error: ' mysql_error());

            }

    ?>
    Now I worked out that there are three
    mysql_query($sql3,$con) or die('Error: ' . mysql_error());
    which is probably the connection, I have changed my $sql, so they are $sql, $sql2, $sql3 so the same naming can't be a problem.

    I originall had
    $result=mysql_query($sql) or die('Error: ' . mysql_error());

    but I changed that to
    mysql_query($sql/2/3,$con) or die('Error: ' . mysql_error());
    in case the three $results were creating the same issue.

    I know the problem is probably very simple, but I can't work it out?

    And everything is echoing out correctly, e.g. only once?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    My INSERT INTO is inserting three times in to my db, and I am not sure why?
    Did you mean to the same table purchases?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    Yes.

    This part inserts three times:

    $sql2="INSERT INTO purchases (Id, fullname, pphoto, category, subcategory, body, date)
    VALUES
    ('{$_SESSION['Id']}','{$_SESSION['fullname']}','{$_SESSION['pphoto']}','country','{$_POST['country']}',' ordered from ',NOW())";
    echo $sql2 . '<br />';
    mysql_query($sql2,$con) or die('Error: ' . mysql_error());

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    function check_input($value){
    Are you calling your function inside a loop or something like?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    No loop, the only thing on that page that I have omitted is the session information.

    My code in my main page is:
    Code:
    <script type="text/javascript">
    function loadXMLDoc(File,ID,Msg){
    if (window.XMLHttpRequest)  {
      xmlhttp=new XMLHttpRequest();
      }
    else {
    	try{
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }	
    }
    
    xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById(ID).innerHTML=xmlhttp.responseText;
        }
      }
    
    var params=Msg;
    xmlhttp.open("POST",File,true);
    
    xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache");
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
    }
    
    </script>
    </head>
    <body>
    		<label class="two">Country:</label>
    			<form action="" method="post">
    			<select name="country"  class="input" required="required">
    			<option value="please select"
    			selected"selected">Please Select</option>
    					<option value="AF|" >Afghanistan</option>
    //country list
    					<option value="ZW|" >Zimbabwe</option>
    
    			</select>
    			&nbsp; &nbsp; 
    			<input type="button" value="Update" onclick="loadXMLDoc('update.php','txtHint','country='+encodeURI(document.getElementById('country').value))"; /> 
    			</form>
    			&nbsp; &nbsp;
    				<span id="txtHint"></span>
    (update.php is the code posted in my first post).

    As you can see this has it's own separate form from any other information.

    I do have other parts of the page refreshing but they are contained.

    The only possible exception is that in my main page I have information in the session refreshing, but that doesn't include any of the information I use to submit. By refreshing I mean I use setInterval.

    The other thing that might help, is that originally I made the mistake of having mysql_query($sql3,$con) or die('Error: ' . mysql_error()); out of the brackets, as below, and when I did that it was inserting the information four times.
    PHP Code:
        $sql="UPDATE store SET country ='{$_POST['country']}' 
        WHERE PId={$_SESSION['Id']}"
    ;

    echo 
    $sql '<br />';

        
    mysql_query($sql,$con) or die('Error: ' mysql_error());


        
    $sql2="INSERT INTO purchases (Id, fullname, pphoto, category, subcategory, body, date)
            VALUES
            ('{$_SESSION['Id']}','{$_SESSION['fullname']}','{$_SESSION['pphoto']}','country','{$_POST['country']}',' ordered from ',NOW())"
    ;
    echo 
    $sql2 '<br />';        
        
    mysql_query($sql2,$con) or die('Error: ' mysql_error());

        if(
    $_SESSION['country']=='online purchase'){
            
    $sql3="UPDATE countryepurchases SET {$_POST['ccountry']}=='{$_POST['ccountry']}'
            WHERE PId={$_SESSION['Id']}"
    ;
    echo 
    $sql3 '<br />';
    }
        
    mysql_query($sql3,$con) or die('Error: ' mysql_error()); 


  •  

    Posting Permissions

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