Updating post issues

Heya, i’m kinda having trouble atm. Here’s what i’m doing. After using my search.php to find a customer’s last name, I click on the “select” button and it gets more information on that customer. It displays properly.

What i’m having trouble is editing/updating. So when I want to change the email, I would edit it, but after clicking update, it won’t change. Then I change the code to use this “$customerID = (int) $_POST[‘customerID’];”, and it’s giving me errors. “Undefined index: customerID.”. The GET method has no problem with customerID. It’s after the while loop post, in the Update query. Nearly finished with this code, trying to fix this problem! Any help is appreciated. Here’s my update_customer php code

   <?php
 if (isset($_GET))  
 if (empty($_GET['customerID'])) {
	    die("No customer ID received!");
	}
         $customerID = (int) $_GET['customerID'];
           
  $db = mysql_connect("localhost", "user", "pw") or die ('I cannot connect to the database  because: ' . mysql_error());
    $mydb=mysql_select_db("dbname");
	
    $customerID = mysql_real_escape_string($customerID);
    
  $sql = "SELECT * FROM customers WHERE customerID =". $customerID.""; 
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result)){
     $lastName = $row['lastName'];
     $firstName = $row['firstName'];
     $address = $row['address'];
     $city = $row['city'];
     $state = $row['state'];
     $postalCode = $row['postalCode'];
     $countryCode = $row['countryCode'];
     $phone = $row['phone'];
     $email = $row['email'];
     $password = $row['password'];
     
      if (isset($_POST)) {
     "UPDATE customers SET lastName='$lastName',firstName='$firstName', address='$address', city = '$city', state = '$state', postalCode = '$postalCode', countryCode = '$countryCode', phone = '$phone', email = '$email', password = '$password' WHERE customerID =". $customerID."";
      $customerID = (int) $_POST['customerID'];
 }
?>
    <h1>View/Update Customer</h1>
    <form action="index.php" method="post" id="customer_form">
        <input type="hidden" name="action" value="update_customer" />
     
        <label>First Name:</label>
        <input type="text" name="firstName" value="<? echo $row['firstName']; ?>" />
        <br />

        <label>Last Name:</label>
        <input type="text" name="lastName" value="<? echo $row['lastName']; ?>"/>
        <br />
        <label>Address:</label>
        <input type="text" name="address"value="<? echo $row['address']; ?>" />
        <br />
        
        <label>City:</label>
        <input type="text" name="city"value="<? echo $row['city']; ?>" />
        <br />
        
        <label>State:</label>
        <input type="text" name="state"value="<? echo $row['state']; ?>" />
        <br />
        
        <label>Postal Code:</label>
        <input type="text" name="postalCode"value="<? echo $row['postalCode']; ?>" />
        <br />
        
        <label>Country Code:</label>
        <input type="text" name="countryCode"value="<? echo $row['countryCode']; ?>" />
        <br />
        
        <label>Phone:</label>
        <input type="text" name="phone"value="<? echo $row['phone']; ?>" />
        <br />
        
        <label>Email:</label>
        <input type="text" name="email"value="<? echo $row['email']; ?>" />
        <br />
        
        <label>Password:</label>
        <input type="text" name="password"value="<? echo $row['password']; ?>" />
        <br />
        
        <label>&nbsp;</label>
        <input type="submit" value="Update Customer" />
        <br />  <br />
    </form>
    <p><a href="index.php?action=list_customers">Search Customers</a></p>

}



<?php

}
?>

It’s probably referencing the column name. Your UPDATE statement should be customerID not $customerID.

you need to put mysql_query() on your UPDATE…

try this:


<?php
if(isset($_GET))
    if(empty($_GET['customerID']))
    {
        die("No customer ID received!");
    }


$customerID = (int) $_GET['customerID'];




$db = mysql_connect("localhost", "user", "pw") or die('I cannot connect to the database  because: ' . mysql_error());
$mydb = mysql_select_db("dbname");


$customerID = mysql_real_escape_string($customerID);


$sql = "SELECT * FROM customers WHERE customerID =" . $customerID . "";
$result = mysql_query($sql);


while($row = mysql_fetch_array($result))
{
    $lastName = $row['lastName'];
    $firstName = $row['firstName'];
    $address = $row['address'];
    $city = $row['city'];
    $state = $row['state'];
    $postalCode = $row['postalCode'];
    $countryCode = $row['countryCode'];
    $phone = $row['phone'];
    $email = $row['email'];
    $password = $row['password'];
?>
    <h1>View/Update Customer</h1>
    <form action="index.php" method="post" id="customer_form">
        <input type="hidden" name="action" value="update_customer" />


        <label>First Name:</label>
        <input type="text" name="firstName" value="<? echo $row['firstName']; ?>" />
        <br />


        <label>Last Name:</label>
        <input type="text" name="lastName" value="<? echo $row['lastName']; ?>"/>
        <br />
        <label>Address:</label>
        <input type="text" name="address"value="<? echo $row['address']; ?>" />
        <br />


        <label>City:</label>
        <input type="text" name="city"value="<? echo $row['city']; ?>" />
        <br />


        <label>State:</label>
        <input type="text" name="state"value="<? echo $row['state']; ?>" />
        <br />


        <label>Postal Code:</label>
        <input type="text" name="postalCode"value="<? echo $row['postalCode']; ?>" />
        <br />


        <label>Country Code:</label>
        <input type="text" name="countryCode"value="<? echo $row['countryCode']; ?>" />
        <br />


        <label>Phone:</label>
        <input type="text" name="phone"value="<? echo $row['phone']; ?>" />
        <br />


        <label>Email:</label>
        <input type="text" name="email"value="<? echo $row['email']; ?>" />
        <br />


        <label>Password:</label>
        <input type="text" name="password"value="<? echo $row['password']; ?>" />
        <br />


        <label>&nbsp;</label>
        <input type="submit" value="Update Customer" />
        <br />  <br />
    </form>
    <p><a href="index.php?action=list_customers">Search Customers</a></p>


    }






    <?php
}


if(isset($_POST['action']))
{
    mysql_query("UPDATE customers SET lastName='$lastName',firstName='$firstName', address='$address', city = '$city', state = '$state', postalCode = '$postalCode', countryCode = '$countryCode', phone = '$phone', email = '$email', password = '$password' WHERE customerID =" . $customerID . "");
}
?>

Not saying this is your exact problem, but could be contributing to it:

      if (isset($_POST)) {

// this line should appear first

      $customerID = (int) $_POST['customerID'];

// php will expand scalar vars inside double quotes
// ASSUMING customerID is an integer, seeing as you typecast it to such, therefore does
// not need quoting either

     "UPDATE customers SET ... snip ... WHERE customerID =$customerID";

 }

thanks for the replies guys. I have to revamp this code a lot. Kinda have lots of issues here. I’ll take all your posts in consideration while reworking this code. thanks!