Help! Can anyone see the problem here

A

Anonymous

Guest
I am trying to write a piece of code which will add customer details.The customer details can be re-entered but will be overwritten on the db table. The problem is that it always writes the updated details as a new row instead of updating the existing entry. Can anyone see the problem with this code?

php:
Code:
<?php

function check_cust() {
        mysql_connect("localhost","username","password");
        $query = "SELECT session FROM orderlog WHERE session='".$cart_id."'"; 
        $numresults = mysql_query($query);
        $numrows=mysql_num_rows($numresults);
        if($numrows == 0) { 
            $yx=0;
    
            } 
            else {$yx=1;
               }
               return $yx;
        }
        

mysql_select_db("dbtable") or die("Unable to select database"); //select which database we're using 

function add_customer($cart_id,$first_name, $last_name, $addr1, $addr2, $addr3, 
$addr4, $addr5, $pcode, $cntry, $email, $message, $ship) {
            check_cust();
          
    if ($yx==0){
    $query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, " 
           . "addr4, addr5, pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', '$last_name', " 
           . "'$addr1', '$addr2', '$addr3', '$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') "; 
            mysql_query($query); 
        
        }
         else {$query = "DELETE FROM orderlog WHERE session='".$this->cart_id."' ";
         mysql_query($query); 
         $query = "INSERT INTO orderlog (session, first_name, last_name, addr1, addr2, addr3, addr4, addr5, " 
                      . "pcode, cntry, email, message, ship) VALUES ('$cart_id', '$first_name', " 
                      . "'$last_name', '$addr1', '$addr2', '$addr3', " 
                      . "'$addr4', '$addr5', '$pcode', '$cntry', '$email', '$message', '$ship') "; 
            mysql_query($query);
         }}
?>
 
You need an UPDATE sql query instead of an INSERT.
Something like:
Code:
<?php

$query = "UPDATE orderlog SET session='$cart_id', first_name='$first_name' ... WHERE id='whatever'";

?>
Note: Please use code tags next time!
 
Back
Top