Hi, I have written a script to update customer details. At the moment I retrieve customer details from the database and display them within the relevant text fields. I then want to change the appropriate data and update the database so the new data is stored. The update works but the problem is, the original value retrieved from the database is saved and not the value changed in the text field. If anyone could help I would be very grateful!!!

Update code and one text field code:

 

if (!isset($_SESSION['customer_id'])) { 

header ("Location:  <a href="http://" target="_blank">[url]http://[/url]</a>" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php"); 
ob_end_clean(); 
exit(); 

} else { 

if (isset($_POST['submit'])) { // Handle the form. 

    require ('../mysql_connect.php'); // Connect to the database. 

// Check for an email address. 
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))) { 
    $e = escape_data($_POST['email']); 
} else { 
    $e = FALSE; 
    echo '<p><font color="red" size="+1">Please enter a valid email address!</font></p>'; 
} 



// Check for addr1: 
if (strlen($_POST['addr1']) > 0) { 
$a1 = stripslashes($_POST['addr1']); 
} else { // If no addr1 was entered... 
$a1 = NULL; 
echo '<p><b>You forgot to enter your address!</b></p>'; 
} 

// Check for addr2: 
if (strlen($_POST['addr2']) > 0) { 
$a2 = stripslashes($_POST['addr2']); 
} else { // If no postcode was entered... 
$a2 = NULL; 
echo '<p><b>You forgot to enter your address!</b></p>'; 
} 

//addr3: 
{ 
$a3 = stripslashes($_POST['addr3']); 
} 

//addr4: 
{ 
$a4 = stripslashes($_POST['addr4']); 
} 

// Check for a postcode. 
if (eregi ("^[[:alnum:]]{6,}$", stripslashes(trim($_POST['postcode'])))) { 
    $pc = escape_data($_POST['postcode']); 
} else { 
    $pc = FALSE; 
    echo '<p><font color="red" size="+1">Please enter your postcode!</font></p>'; 
} 

//telephone: 
{ 
$te = stripslashes($_POST['telephone']); 
} 

    if ($e && $a1 && $a2 && $pc && $te) { // If everything's OK. 

        // Make the query. 
        $query = "UPDATE customer SET (email, addr1, addr2, addr3, addr4, postcode, telephone) VALUES ('$e', '$a1', '$a2', '$a3', '$a4', '$pc', '$te')  WHERE customer_id={$_SESSION['customer_id']}";         
        $result = @mysql_query ($query); // Run the query. 
        if (mysql_affected_rows() != 0) { // If it ran OK. 

            // Send an email, if desired. 
            echo '<h3>Your details have been changed.</h3>'; 
            include ('includes/footer.html'); // Include the HTML footer. 
            exit();                 

        } else { // If it did not run OK. 

            // Send a message to the error log, if desired. 
            echo "<p><font color='red' size='+1'>Your details could not be changed due to a system error. We apologize for any inconvenience.</font></p>";  
        }         
        mysql_close(); // Close the database connection. 

    } else { // Failed the validation test. 
        echo '<p><font color="red" size="+1">Please try again.</font></p>';         
    } 

} else { // Display the form. // End of the main Submit conditional. 
require ('../mysql_connect.php'); // Connect to the database.     
?> <?php // Retrieve the addr4 and add to field. $sql = "SELECT addr1 FROM customer WHERE customer.customer_id = {$_SESSION['customer_id']}";
$result = @mysql_query($sql) or die("Couldn't execute query!");
while ($row = mysql_fetch_array($result)) {
$addr1 = $row['addr1'];
echo "<input type='text' name='addr1' size='40' maxlength='40' value='$addr1'>";
}
?>

    Here is the one I ues.

    Code provided bySpechal.com

    
    //form page
    
    <? session_start();
    $username = $_SESSION['username@spechal.com'];
    if(!isset($_SESSION['username@spechal.com'])){
    echo "<font face='Tahoma' size='2'><b><center>Error! <br><br>
    Either you chose not to accept the cookie or your browser is set to reject cookies and/or sessions.
    </b></font>";
    exit();
    } else { 
    include('css.php');
    include('securemenu.php'); 
    include('dbinfo.inc.php');
    $q = mysql_query("SELECT * FROM customers WHERE username='$username'") or die(mysql_error());
    extract(mysql_fetch_array($q));
    ?>
    <form action="updateinfo.php" method="post" name="register" id="register">
      <table width="100%" border="0">
        <tr> 
          <td width="23%" height="25"><strong><font size="2" face="Tahoma">First Name: 
            </font></strong></td>
          <td width="77%"><strong><font size="2" face="Tahoma"> 
            <input name="fname" type="text" id="fname" value="<?= $fname ?>" size="24" maxlength="12">
            * </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Last Name: </font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="lname" type="text" id="lname" value="<?= $lname ?>" size="24" maxlength="24">
            * </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Street Address: </font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="addy" type="text" id="addy" value="<?= $addy ?>" size="24" maxlength="36">
            </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">City:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="city" type="text" id="city" value="<?= $city ?>" size="24" maxlength="12">
            </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">State or Province:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="state" type="text" id="state" value="<?= $state ?>" size="24" maxlength="24">
            </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Zip Code:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="zip" type="text" id="zip" value="<?= $zip ?>" size="24" maxlength="8">
            </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Valid Email Address:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="email1" type="text" id="email1" value="<?= $email ?>" size="24" maxlength="63">
            * </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Confirm Email Address:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="email2" type="text" id="email2" value="<?= $email ?>" size="24" maxlength="63">
            * </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Phone Number</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="phone" type="text" id="phone" value="<?= $phone ?>" size="24" maxlength="18">
            * </font></strong></td>
        </tr>
        <tr> 
          <td><strong><font size="2" face="Tahoma">Fax Number:</font></strong></td>
          <td><strong><font size="2" face="Tahoma"> 
            <input name="fax" type="text" id="fax" value="<?= $fax ?>" size="24" maxlength="18">
            </font></strong></td>
        </tr>
        <tr>
          <td> 
            <?
      if (getenv("HTTP_X_FORWARDED_FOR")) { 
        $ip = getenv("HTTP_X_FORWARDED_FOR"); 
    } else { 
        $ip = getenv("REMOTE_ADDR"); 
    } 
    ?>
            <input type="hidden" name="ip" value="<?= $ip ?>"> <font size="2" face="Tahoma"><strong>&nbsp;*required</strong></font></td>
        </tr>
        <tr>
          <td><input type="image" src="images/submit.jpg" value="Submit Form"></td>
          <td></td>
        </tr>
      </table>
    
    </form>
    <? } ?>
    
    

      SCRIPT PAGE

      <?
      session_start();
      $username = $_SESSION['username@spechal.com'];
      if(!isset($_SESSION['username@spechal.com'])){
      echo "<font face='Tahoma' size='2'><b><center>Error! <br><br>
      Either you chose not to accept the cookie or your browser is set to reject cookies and/or sessions.
      </b></font>";
      exit();
      } else {
      include('dbinfo.inc.php');
      include('css.php');
      include('functions.inc.php');
      
      $fname = $_POST['fname'];
      $lname = $_POST['lname'];
      $addy = $_POST['addy'];
      $city = $_POST['city'];
      $state = $_POST['state'];
      $zip = $_POST['zip'];
      $email1 = $_POST['email1'];
      $email2 = $_POST['email2'];
      $phone = $_POST['phone'];
      $fax = $_POST['fax'];
      if (getenv ("HTTP_X_FORWARDED_FOR")) { 
        $ip = getenv ("HTTP_X_FORWARDED_FOR"); 
      } else { 
        $ip = getenv ("REMOTE_ADDR"); 
      } 
      if($phone == '' || $email1 == '' || $email2 == '' || $fname == '' || $lname == ''){ 
      echo "Error.  Please fill out all required fields. <br>"; 
      ?> <form>
      <input type="button" value="<-Back" onclick="history.back();">
      </form>
      <? } elseif($email1!=$email2){
      echo "Error.  Email addresses do not match. <br>"; 
      ?> <form>
      <input type="button" value="<-Back" onclick="history.back();">
      </form>
      <? }  else {
      $fname = $_POST['fname'];
      $lname = $_POST['lname'];
      $addy = $_POST['addy'];
      $city = $_POST['city'];
      $state = $_POST['state'];
      $zip = $_POST['zip'];
      $email = $_POST['email1'];
      $phone = $_POST['phone'];
      $fax = $_POST['fax'];
      
      $password = crypt_md5($password, $key);
      mysql_query("UPDATE customers SET fname='$fname', lname='$lname', addy='$addy', city='$city', state='$state', zip='$zip', email='$email', phone='$phone', fax='$fax', ip='$ip' WHERE username='$username'") or die(mysql_error());
      echo "<center>";
      include('securemenu.php');
      echo "<font color='#000000' size='2' face='Tahoma'><b>";
      echo "<br><br>";
      print"<div align=\"center\">Thank you for updating your information $fname $lname <br>
      "; 
      echo "</font></b>";
      }}
      ?> 
      

        Hey thanks for that, had a play around and worked out it was because of my UPDATE command. I used the way you had done it and it worked for the rest of my code!!
        Thanks again.
        KEv

          Write a Reply...