Index
    Preface
      What This Book Is About
      What You Need to Know
      How This Book Is Organized
      How to Use This Book
      Conventions Used in This Book
      Using Code Examples
      How to Contact Us
      Web Site and Code Examples
      Acknowledgments
      Chapter 1.  Database Applications and the Web
      Section 1.1.  The Web
      Section 1.2.  Three-Tier Architectures
      Chapter 2.  The PHP Scripting Language
      Section 2.1.  Introducing PHP
      Section 2.2.  Conditions and Branches
      Section 2.3.  Loops
      Section 2.4.  Functions
      Section 2.5.  Working with Types
      Section 2.6.  User-Defined Functions
      Section 2.7.  A Working Example
      Chapter 3.  Arrays, Strings, and Advanced Data Manipulation in PHP
      Section 3.1.  Arrays
      Section 3.2.  Strings
      Section 3.3.  Regular Expressions
      Section 3.4.  Dates and Times
      Section 3.5.  Integers and Floats
      Chapter 4.  Introduction to Object-Oriented Programming with PHP 5
      Section 4.1.  Classes and Objects
      Section 4.2.  Inheritance
      Section 4.3.  Throwing and Catching Exceptions
      Chapter 5.  SQL and MySQL
      Section 5.1.  Database Basics
      Section 5.2.  MySQL Command Interpreter
      Section 5.3.  Managing Databases and Tables
      Section 5.4.  Inserting, Updating, and Deleting Data
      Section 5.5.  Querying with SQL SELECT
      Section 5.6.  Join Queries
      Section 5.7.  Case Study: Adding a New Wine
      Chapter 6.  Querying Web Databases
      Section 6.1.  Querying a MySQL Database Using PHP
      Section 6.2.  Processing User Input
      Section 6.3.  MySQL Function Reference
      Chapter 7.  PEAR
      Section 7.1.  Overview
      Section 7.2.  Core Components
      Section 7.3.  Packages
      Chapter 8.  Writing to Web Databases
      Section 8.1.  Database Inserts, Updates, and Deletes
      Section 8.2.  Issues in Writing Data to Databases
      Chapter 9.  Validation with PHP and JavaScript
      Section 9.1.  Validation and Error Reporting Principles
      Section 9.2.  Server-Side Validation with PHP
      Section 9.3.  JavaScript and Client-Side Validation
      Chapter 10.  Sessions
      Section 10.1.  Introducing Session Management
      Section 10.2.  PHP Session Management
      Section 10.3.  Case Study: Using Sessions in Validation
      Section 10.4.  When to Use Sessions
      Section 10.5.  PHP Session API and Configuration
      Chapter 11.  Authentication and Security
      Section 11.1.  HTTP Authentication
      Section 11.2.  HTTP Authentication with PHP
      Section 11.3.  Form-Based Authentication
      Section 11.4.  Protecting Data on the Web
      Chapter 12.  Errors, Debugging, and Deployment
      Section 12.1.  Errors
      Section 12.2.  Common Programming Errors
      Section 12.3.  Custom Error Handlers
      Chapter 13.  Reporting
      Section 13.1.  Creating a Report
      Section 13.2.  Producing PDF
      Section 13.3.  PDF-PHP Reference
      Chapter 14.  Advanced Features of Object-Oriented Programming in PHP 5
      Section 14.1.  Working with Class Hierarchies
      Section 14.2.  Class Type Hints
      Section 14.3.  Abstract Classes and Interfaces
      Section 14.4.  Freight Calculator Example
      Chapter 15.  Advanced SQL
      Section 15.1.  Exploring with SHOW
      Section 15.2.  Advanced Querying
      Section 15.3.  Manipulating Data and Databases
      Section 15.4.  Functions
      Section 15.5.  Automating Querying
      Section 15.6.  Table Types
      Section 15.7.  Backup and Recovery
      Section 15.8.  Managing Users and Privileges
      Section 15.9.  Tuning MySQL
      Chapter 16.  Hugh and Dave's Online Wines:A Case Study
      Section 16.1.  Functional and System Requirements
      Section 16.2.  Application Overview
      Section 16.3.  Common Components
      Chapter 17.  Managing Customers
      Section 17.1.  Code Overview
      Section 17.2.  Customer Validation
      Section 17.3.  The Customer Form
      Chapter 18.  The Shopping Cart
      Section 18.1.  Code Overview
      Section 18.2.  The Winestore Home Page
      Section 18.3.  The Shopping Cart Implementation
      Chapter 19.  Ordering and Shipping at the Online Winestore
      Section 19.1.  Code Overview
      Section 19.2.  Credit Card and Shipping Instructions
      Section 19.3.  Finalizing Orders
      Section 19.4.  HTML and Email Receipts
      Chapter 20.  Searching and Authentication in the Online Winestore
      Section 20.1.  Code Overview
      Section 20.2.  Searching and Browsing
      Section 20.3.  Authentication
      Appendix A.  Linux Installation Guide
      Section A.1.  Finding Out What's Installed
      Section A.2.  Installation Overview
      Section A.3.  Installing MySQL
      Section A.4.  Installing Apache
      Section A.5.  Installing PHP
      Section A.6.  What's Needed for This Book
      Appendix B.  Microsoft Windows Installation Guide
      Section B.1.  Installation Overview
      Section B.2.  Installing with EasyPHP
      Section B.3.  What's Needed for This Book
      Appendix C.  Mac OS X Installation Guide
      Section C.1.  Getting Started
      Section C.2.  Installing MySQL
      Section C.3.  Setting Up Apache and PHP
      Section C.4.  What's Needed for This Book
      Appendix D.  Web Protocols
      Section D.1.  Network Basics
      Section D.2.  Hypertext Transfer Protocol
      Appendix E.  Modeling and Designing Relational Databases
      Section E.1.  The Relational Model
      Section E.2.  Entity-Relationship Modeling
      Appendix F.  Managing Sessions in theDatabase Tier
      Section F.1.  Using a Database to Keep State
      Section F.2.  PHP Session Management
      Section F.3.  MySQL Session Store
      Appendix G.  Resources
      Section G.1.  Client Tier Resources
      Section G.2.  Middle-Tier Resources
      Section G.3.  Database Tier Resources
      Section G.4.  Security and Cryptography Resources
      Appendix H.  The Improved MySQL Library
      Section H.1.  New Features
      Section H.2.  Getting Started
      Section H.3.  Using the New Features
    Colophon
    Copyright



 

Previous Section  < Day Day Up >  Next Section

17.3 The Customer Form

The script customer/details.php is shown in Example 17-2. If the user is logged in and validation has not previously failed, the customer data is retrieved from the customer table and used to populate the form widgets. If the user isn't logged in, and validation has not previously failed, a blank form is shown to collect new member details. If data has failed validation, the form is repopulated and the error messages are displayed. Error messages and previously entered form values are stored by the customer/validate.php script in the session array variables $_SESSION["custFormVars"] and $_SESSION["custErrors"] respectively.

The customer form and its widgets are displayed using our winestoreFormTemplate class that's derived from the PEAR ITX template class. It's discussed in detail in Chapter 16.

Example 17-2. The customer form script customer/details.php
<?php

require_once "DB.php";

require_once "../includes/winestore.inc";

require_once "../includes/authenticate.inc";

require_once "../includes/template.inc";



set_error_handler("customHandler");



// Show meaningful instructions for UPDATE or INSERT

if (isset($_SESSION["loginUsername"]))

   $instructions = "Please amend your details below as required.";

else

   $instructions = "Please fill in the details below to join.";



// Takes <form> heading, instructions, action, formVars name, 

// and formErrors name as parameters

$template = new winestoreFormTemplate("Customer Details",

                $instructions, S_VALIDATE, "custFormVars", "custErrors");



session_start( );



$connection = DB::connect($dsn, true);

if (DB::isError($connection))

  trigger_error($connection->getMessage( ), E_USER_ERROR);



// Is the user logged in and are there no errors from previous

// validation?  If so, look up the customer for editing

if (isset($_SESSION["loginUsername"]) &&

    !isset($_SESSION["custErrors"]))

{

   // Check the user is properly logged in

   sessionAuthenticate(S_MAIN);



   $query = "SELECT title_id, surname, firstname, initial, address,

                    city, state, zipcode, country_id, phone,

                    birth_date

             FROM users, customer

             WHERE users.cust_id = customer.cust_id

             AND user_name = '{$_SESSION["loginUsername"]}'";



   $result = $connection->query($query);

   if (DB::isError($result))

      trigger_error($result->getMessage( ), E_USER_ERROR);

   $row = $result->fetchRow(DB_FETCHMODE_ASSOC);



   // Reset $_SESSION["custFormVars"], since we're loading

   // from the customer table

   $_SESSION["custFormVars"] = array( );



   // Load all the <form> widgets with customer data

   foreach($row as $variable => $value)

      $_SESSION["custFormVars"]["{$variable}"] = $value;

}



// Load the titles from the title table

$titleResult = $connection->query("SELECT * FROM titles");

if (DB::isError($titleResult))

   trigger_error($titleResult->getMessage( ), E_USER_ERROR);



// Load the countries from the country table

$countryResult = $connection->query("SELECT * FROM countries");

if (DB::isError($countryResult))

   trigger_error($countryResult->getMessage( ), E_USER_ERROR);



// Create widgets for each of the customer fields

$template->selectWidget("title_id", "Title:",

                        "title", $titleResult);

$template->mandatoryWidget("firstname", "First name:", 50);

$template->mandatoryWidget("surname", "Surname:", 50);

$template->optionalWidget("initial", "Middle initial:", 1);

$template->mandatoryWidget("address", "Address:", 50);

$template->mandatoryWidget("city", "City:", 50);

$template->optionalWidget("state", "State:", 20);

$template->mandatoryWidget("zipcode", "Zip code:", 10);

$template->selectWidget("country_id", "Country:",

                        "country", $countryResult);

$template->optionalWidget("phone", "Telephone:", 15);

$template->mandatoryWidget("birth_date", 

                           "Date of Birth (dd/mm/yyyy):", 10);



// Only show the username/email and password widgets to new users

if (!isset($_SESSION["loginUsername"]))

{

   $template->mandatoryWidget("loginUsername", "Email/username:", 50);

   $template->passwordWidget("loginPassword", "Password:", 15);

}



// Add buttons and messages, and show the page

$template->showWinestore(NO_CART, B_ALL & ~B_EMPTY_CART & ~B_UPDATE_CART &

                ~B_PURCHASE & ~B_DETAILS & ~B_LOGINLOGOUT)



?>

17.3.1 The Customer Receipt Page

Example 17-3 shows the customer receipt script, customer/receipt.php, that is called after a database write to insert or update a customer. The script is a receipt page that can be bookmarked and the script only reads details from the database. Reloading of the page therefore has no undesirable side effects.

Example 17-3. The customer/receipt.php customer receipt page
<?php

// This script shows the user a receipt for their customer

// UPDATE or INSERT. It carries out no database writes and

// can be bookmarked.

// The user must be logged in to view it.



require_once "DB.php";

require_once "../includes/winestore.inc";

require_once "../includes/authenticate.inc";

require_once "../includes/template.inc";



set_error_handler("customHandler");



// Show the user a customer INSERT or UPDATE receipt

function show_HTML_receipt($cust_id, $connection, &$template)

{

  // Retrieve the customer details

  $query = "SELECT * FROM customer WHERE cust_id = {$cust_id}";

  $result = $connection->query($query);

  if (DB::isError($result))

     trigger_error($result->getMessage( ), E_USER_ERROR);

  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);



  // Is there an optional phone field? If so, add it to the output

  if (!empty($row["phone"]))

  {

     $template->setCurrentBlock("phone");

     $template->setVariable("PHONE", $row["phone"]);

     $template->parseCurrentBlock("address");

  }



  // Now, add all the mandatory fields to the output

  $template->setCurrentBlock( );

  $template->setVariable("EMAIL", $_SESSION["loginUsername"]);

  $template->setVariable("FIRSTNAME", $row["firstname"]);

  $template->setVariable("SURNAME", $row["surname"]);

  $template->setVariable("INITIAL", $row["initial"]);

  $template->setVariable("ADDRESS", $row["address"]);

  $template->setVariable("CITY", $row["city"]);

  $template->setVariable("STATE", $row["state"]);

  $template->setVariable("ZIPCODE", $row["zipcode"]);

  $template->setVariable("DOB", $row["birth_date"]);

  $template->setVariable("CUSTTITLE", showTitle($row["title_id"],

                         $connection));

  $template->setVariable("COUNTRY", showCountry($row["country_id"],

                         $connection));

}



// -----



session_start( );



$connection = DB::connect($dsn, true);

if (DB::isError($connection))

  trigger_error($connection->getMessage( ), E_USER_ERROR);



// Check the user is properly logged in

sessionAuthenticate(S_MAIN);



// Find out the cust_id of the user

$cust_id = getCust_id($_SESSION["loginUsername"]);



// Start a new page

$template = new winestoreTemplate(T_CUSTRECEIPT);



// Show the customer confirmation

show_HTML_receipt($cust_id, $connection, $template);



// Add buttons and messages, and show the page

$template->showWinestore(NO_CART, B_HOME);

?>

Customers can view their receipts only when logged in. The cust_id of the customer is again retrieved using the function getCust_id( ) that's discussed in Chapter 16.

The receipt script populates the templates/custreceipt.tpl template shown in Example 17-4. The receipt page uses the winestoreTemplate class that's discussed in Chapter 16 to provide the HTML framework for displaying the page and to show messages to the user. The templates/custreceipt.tpl template is therefore only part of the body of the page.

Example 17-4. The templates/custreceipt.tpl customer receipt template
h1>Account details for <font color="red">{EMAIL}</font></h1>



<p><i>Please record your password somewhere safe for future use.</i>



<p>Your shipping and billing details are as follows:

<br><b>{CUSTTITLE}&nbsp;{FIRSTNAME}&nbsp;{INITIAL}&nbsp;{SURNAME}

<br>{ADDRESS}

<br>{CITY} {STATE} {ZIPCODE}

<br>{COUNTRY}</b>

<br>

<!-- BEGIN phone -->

<br><b>Telephone: {PHONE}

<!-- END phone -->

<br><b>Date of Birth: {DOB}</b>

<br>

<br>

    Previous Section  < Day Day Up >  Next Section







    Copyright © 2010 | Domen maybe sale - bye this domen