Source of: ../Wda42/example.8-5.php

<?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
  
include 'db.inc';
  include 
'error.inc';

  function 
fieldError($fieldName$errors)
  {
     if (isset(
$errors[$fieldName]))
        echo 
        
"<font color=RED>$errors[$fieldName]</font><br>";
  }    

  
// Connect to a session.
  // Up to three session variables can be registered:
  // (1) $formVars - previously entered data that has failed validation
  // (2) $errors - an array of error messages, up to one per widget
  // (3) $custID - the customer ID of a customer to edit
  
session_start();

  
// Retrieve the custID from the session variable (if set)
  
if (session_is_registered("custID"))
     
$custID $_SESSION["custID"];

  
// $custID can also be passed as a GET parameter
  // If it is, override any session variable
  
if (!empty($_GET["custID"]))
     
$custID clean($_GET["custID"], 5);

  
// Initialise $formVars from the $_SESSION["formVars"] (if set)
  
if (session_is_registered("formVars"))
  {
     
$row $_SESSION["formVars"];
     
$formVars["surname"] = $row["surname"];
     
$formVars["firstName"] = $row["firstName"];
     
$formVars["address1"] = $row["address1"];
     
$formVars["city"] = $row["city"];
     
$formVars["email"] = $row["email"];
     
$formVars["dob"] = $row["dob"];
  }

  
// Has a custID been provided and are there no errors? 
  // If so, retrieve the customer details for editing.
  
if (!empty($custID) && empty($_SESSION["errors"]))
  {
     
// Register the custID as a session variable
     
if (!session_is_registered("custID"))
        
session_register("custID");
     
$_SESSION["custID"] = $custID;

     if (!(
$connection = @ mysql_pconnect($hostName
                                         
$username
                                         
$password)))
        die(
"Could not connect to database");

     if (!
mysql_select_db($databaseName$connection))
        
showerror();
  
     
$query "SELECT * FROM customer 
               WHERE cust_id = " 
$custID;
  
     if (!(
$result = @ mysql_query($query$connection)))
        
showerror();
  
     
$row mysql_fetch_array($result);

     
// Set up a $formVars array with the POST variables
     // and register with the session.
     
if (!session_is_registered("formVars"))
        
session_register("formVars");
  
     
// Reset $formVars, since we're loading from 
     // the customer table
     
$formVars = array();
  
     
// Load all the form variables with customer data
     
$formVars["surname"] = $row["surname"];
     
$formVars["firstName"] = $row["firstname"];
     
$formVars["address1"] = $row["addressline1"];
     
$formVars["city"] = $row["city"];
     
$formVars["email"] = $row["email"];
     
$formVars["dob"] = substr($row["birth_date"], 82) . "/" .
                        
substr($row["birth_date"], 52) . "/" .   
                        
substr($row["birth_date"], 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">  
<form method="post" action="example.8-4.php">
<h1>Customer Details</h1>
<?php
  
// Show meaningful instructions for UPDATE or INSERT
  
if (!empty($custID))
     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">First name:</font></td>
   <td><?php echo fieldError("firstName"$_SESSION["errors"]); ?>
       <input type="text" name="firstName" 
        value="<?php echo $formVars["firstName"]; ?>" size=50></td>
</tr>

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

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

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

<tr><td><font color="red">Email/username:</font></td>
   <td><?php echo fieldError("email"$_SESSION["errors"]); ?>
   <input type="text" name="email" 
    value="<?php echo $formVars["email"]; ?>" size=30><td>
</tr>

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

</table><br>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>


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