Source of: example.customer.2.php

<?
/*
Source code example for Web Database Applications

Unless otherwise stated, the source code distributed with this book can be
redistributed in source or binary form so long as an acknowledgment appears
in derived source files.
The citation should list that the code comes from Hugh E.
Williams and David Lane, "Web Database Application with PHP and MySQL"
published by O'Reilly & Associates.
This code is under copyright and cannot be included in any other book,
publication, or educational product without permission from O'Reilly &
Associates.
No warranty is attached; we cannot take responsibility for errors or fitness
for use.
*/
?>
<?php
  
// This script shows the user a customer <form>.
  // It can be used both for INSERTing a new customer and
  // for UPDATE-ing an existing customer. If the customer 
  // is logged in, then it is an UPDATE; otherwise, an 
  // INSERT.
  // The script also shows error messages above widgets 
  // that contain erroneous data; errors are generated 
  // by example.customer.1.php
 
  
include 'include.inc';

  
set_error_handler("errorHandler");

  
// Show an error in a red font
  
function fieldError($fieldName$errors)
  {
    if (isset(
$errors[$fieldName]))
       echo 
"<font color=\"red\">" .
           
$errors[$fieldName] .
           
"</font><br>";
  }    
  
  
// Connect to a session
  
session_start();
  
  
// Is the user logged in and were there no errors from a previous
  // validation?  If so, look up the customer for editing
  
if (session_is_registered("loginUsername") && empty($errors))
  {
     if (!(
$connection = @ mysql_pconnect($hostName
                                         
$username
                                         
$password)))
        
showerror(); 

     if (!
mysql_select_db($databaseName$connection))
        
showerror();
  
     
$custID getCustomerID($loginUsername$connection);
  
     
$query "SELECT * FROM customer 
               WHERE cust_id = " 
$custID;
  
     if (!(
$result = @ mysql_query($query$connection)))
        
showerror();
  
     
$row mysql_fetch_array($result);
  
     
// Reset $formVars, since we're loading from 
     // the customer table
     
$formVars = array();
  
     
// Reset the errors
     
$errors = array();
  
     
// Load all the form variables with customer data
     
$formVars["title"] = $row["title"];
     
$formVars["surname"] = $row["surname"];
     
$formVars["firstName"] = $row["firstname"];
     
$formVars["initial"] = $row["initial"];
     
$formVars["address1"] = $row["addressline1"];
     
$formVars["address2"] = $row["addressline2"];
     
$formVars["address3"] = $row["addressline3"];
     
$formVars["city"] = $row["city"];
     
$formVars["state"] = $row["state"];
     
$formVars["zipcode"] = $row["zipcode"];
     
$formVars["country"] = $row["country"];
     
$formVars["phone"] = $row["phone"];
     
$formVars["fax"] = $row["fax"];
     
$formVars["email"] = $row["email"];
     
$formVars["dob"] = $row["birth_date"];
     
$formVars["dob"] = substr($formVars["dob"], 82) . "/" .
                        
substr($formVars["dob"], 52) . "/" .   
                        
substr($formVars["dob"], 04);
  
  } 
  
?>
<!DOCTYPE HTML PUBLIC 
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head><title>Customer Details</title></head>
<body bgcolor="white">  
<?php
  
// Show the user login status
  
showLogin();
?>
<form method="post" action="example.customer.1.php">
<h1>Customer Details</h1>
<?php
  
// Display any messages to the user
  
showMessage();

  
// Show meaningful instructions for UPDATE or INSERT
  
if (session_is_registered("loginUsername"))
     echo 
"<h3>Please amend your details below as required. Fields shown in <font color=\"red\">red</font> are mandatory.</h3>";
  else
     echo 
"<h3>Please fill in the details below to join. Fields shown in <font color=\"red\">red</font> are mandatory.</h3>";
?>
<table>
<col span="1" align="right">

    <tr><td><font color="red">Title:</font></td>
   <td><select name="title">
       <option <?if ($formVars["title"]=="Mr") echo "selected";?>>Mr
       <option <?if ($formVars["title"]=="Mrs") echo "selected";?>>Mrs
       <option <?if ($formVars["title"]=="Ms") echo "selected";?>>Ms
       <option <?if ($formVars["title"]=="Dr") echo "selected";?>>Dr
       </select><br></td>
    </tr>

    <tr><td><font color="red">First name:</font></td>
   <td><? echo fieldError("firstName"$errors); ?>
       <input type="text" name="firstName" 
      value="<? echo $formVars["firstName"]; ?>" size=50></td>
    </tr>

    <tr><td><font color="red">Surname:</font></td>
   <td><? echo fieldError("surname"$errors); ?>
       <input type="text" name="surname" 
      value="<? echo $formVars["surname"]; ?>" size=50></td>
    </tr>

    <tr><td>Initial: </td>
   <td><? echo fieldError("initial"$errors); ?>
       <input type="text" name="initial" 
      value="<? echo $formVars["initial"]; ?>" size=1></td>
    </tr>

    <tr><td><font color="red">Address:</font></td>
   <td><? echo fieldError("address"$errors); ?>
       <? echo fieldError("address1"$errors); ?>
       <input type="text" name="address1" 
      value="<? echo $formVars["address1"]; ?>" size=50></td>
    </tr>

    <tr><td></td>
   <td><? echo fieldError("address2"$errors); ?>
       <input type="text" name="address2" 
      value="<? echo $formVars["address2"]; ?>" size=50></td>
    </tr>

    <tr><td></td>
   <td><? echo fieldError("address3"$errors); ?>
       <input type="text" name="address3" 
      value="<? echo $formVars["address3"]; ?>" size=50></td>
    </tr>

    <tr><td><font color="red">City:</font></td>
   <td><? echo fieldError("city"$errors); ?>
       <input type="text" name="city" 
      value="<? echo $formVars["city"]; ?>" size=20></td>
    </tr>

    <tr><td>State: </td>
    <td><? echo fieldError("state"$errors); ?>
   <input type="text" name="state" 
       value="<? echo $formVars["state"]; ?>" size=20></td>
    </tr>

    <tr><td><font color="red">Zipcode:</font></td>
    <td><? echo fieldError("zipcode"$errors); ?>
   <input type="text" name="zipcode" 
         value="<? echo $formVars["zipcode"]; ?>" size=5></td>
    </tr>

    <tr><td>Country: </td>
   <td><? echo fieldError("country"$errors); ?>
       <select name="country">
      <option <?if ($formVars["country"]=="Australia") echo "selected";?>>
          Australia
      <option <?if ($formVars["country"]=="United States") echo "selected";?>>
          United States
      <option <?if ($formVars["country"]=="Zimbabwe") echo "selected";?>>
          Zimbabwe
       
      <option <?if ($formVars["country"]=="Deutschland") echo "selected";?>>
          Deutschland
       </select></td>
    </tr>

    <tr><td>Telephone: </td>
   <td><? echo fieldError("phone"$errors); ?>
   <input type="text" name="phone" 
       value="<? echo $formVars["phone"]; ?>" size=15></td>
    </tr>

    <tr><td>Fax: </td>
   <td><? echo fieldError("fax"$errors); ?>
       <input type="text" name="fax" 
      value="<? echo $formVars["fax"]; ?>" size=15></td>
    </tr>

    <tr><td><font color="red">Date of birth (dd/mm/yyyy):</font> </td>
   <td><? echo fieldError("dob"$errors); ?>
       <input type="text" name="dob" 
      value="<? echo $formVars["dob"]; ?>" size=10></td>
    </tr>

<?php
  
// Only show the username/email and password widgets to new users
  
if (!session_is_registered("loginUsername"))
  {
?>
    <tr><td><font color="red">Email/username:</font></td>
   <td><? echo fieldError("email"$errors); ?>
       <input type="text" name="email" 
      value="<? echo $formVars["email"]; ?>" size=50></td>
    </tr>

    <tr><td><font color="red">Password:</font></td>
   <td><? echo fieldError("loginPassword"$errors); ?>
       <input type="password" name="loginPassword" 
      value="<? echo $formVars["loginPassword"]; ?>" size=8></td>
    </tr>
<?php
  
}
?>
<tr>
   <td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
<br><a href="http://validator.w3.org/check/referer"><img
     src="http://www.w3.org/Icons/valid-html401" height="31" width="88"
          align="right" border="0" alt="Valid HTML 4.01!"></a>
</body>
</html>


© 2000--2001 Hugh E. Williams and David Lane.
Valid HTML 4.01!