PHP and MySQL for Client-Server Database Interaction Chapter 10.

Post on 11-Jan-2016

238 views 2 download

transcript

PHP and MySQL for Client-Server Database Interaction

Chapter 10

Overview and Objectives:New Functionality

• Browse through our product catalog without registering (that is, “anonymously”)

• Open an account by registering with our website• Log in to our website as a registered user, and later

log out• Browse through our product catalog as a registered

user, order various items online, and manage a shopping cart

• Purchase the shopping cart items and then check out

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Skills That Will Be Learned

• How a PHP script connects to a MySQL database

• How a PHP script issues queries about customers or products to a MySQL database and then receives and processes the replies

• More about PHP arrays• How to use a PHP session to keep track of a

user’s activity during a visit to our website

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Current State of Our Website

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.Inset © Photos.com

Bird's Eye View of e-store Functionality

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

PHP Code for Connecting to MySQL<?php //db.php $db_location = "localhost"; $db_username = "webbook"; $db_password = "secret"; $db_database = "webbook"; $db_connection = mysql_connect("$db_location","$db_username","$db_password") or die ("Error - Could not connect to MySQL Server"); $db = mysql_select_db($db_database,$db_connection) or die ("Error - Could not open database");?>

Information supplied:• The location of the MySQL server ($db location)• The user’s MySQL username ($db username)• The user’s password ($db password)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Important Start

Registering for Our Web Site

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

You can go to the CD and get this page as a starting point, then edit it.

Registration Scenarios

• During the registration process we must be prepared to deal with at least the following possible scenarios:– Successful registration– Does not pass JavaScript validation on the client

side– Already registered and has perhaps forgotten they

have done so, which will be recognized when the user is discovered to be in the database already

– Choosing a login name that is already in useChapter 10: PHP and MySQL for Client-Server Database Interaction

XHTML and PHP Code for the Registration Form

Chapter 10: PHP and MySQL for Client-Server Database Interaction

These are .php files

Validating the Form Data (1 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Validating the Form Data (2 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Validating the Form Data (3 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Chapter 10: PHP and MySQL for Client-Server Database Interaction

The php files for the customer registration process

db.php

Simply opens the database connection and saves it as a variable

processRegistration.php

This does the actual query to save the data to the database. Also contains the functions to handle the exceptions

registration.phpCalls the processRegistration script/page and provides a place to return to when the processing is done. Not very big

register.phpThis is the page that the user will enter into the url to open up. It all starts from here.It has the form in it to collect the data. (also calls validation)

I suggest we start with trying to add someone to our customer table.

Messages

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Re-registering for the Site with Different e-mail but Same Username

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

XHTML and PHP Code for Processing the Registration Data

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Built in function to start maintaining the idea “state” or “session” variables.

Main Program from processRegistration.php (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Main Program from processRegistration.php (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Check next slide to use the $_POST superglobal to actual grab the data the user typed in to the form.

Chapter 10: PHP and MySQL for Client-Server Database Interaction

VALUES ( NULL, '$_POST[firstName]', '$_POST[middleInitial]', '$_POST[lastName]', '$_POST[email]', '$unique_login', '$_POST[loginPassword]', '$_POST[phone]', '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zip]', '$_POST[country]');";

I believe this correctNotice the addition of “ ‘$_POST[…….]’”

A Helper Function from processRegistration.php

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Another Helper Function from processRegistration.php

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Arraysphp > //The following command creates an array:php > $a[] = "Hello";php > //We omitted the subscript so the next availablephp > //subscript is used, which is 0 in this case.

Chapter 10: PHP and MySQL for Client-Server Database Interaction

php > //Cannot use the echo command to print an array.

php > echo $a;Arrayphp > //But ... the echo command can print elements of an array.

php > echo $a[0];Hello

PHP Arraysphp > //Let us now add a number of elements to our array.

php > $a[] = "World";php > $a[-90] = "This is wild";php > $a[90] = "This is wild";php > $a[70] = "This is wild";php > $a[name] = "Pawan";

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Arraysphp > //You can use the print_r() function to print an entire array.

php > print_r($a);Array([0] => Hello[1] => World[-90] => This is wild[90] => This is wild[70] => This is wild[name] => Pawan)php > //As we can see, the indexes are not ordered as you would havephp > //expected, since -90 comes after 1, and 70 comes after 90.

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Arraysphp > //You can delete an element using the function unset

php > unset($a[-90]);php > print_r($a);Array([0] => Hello[1] => World[90] => This is wild[70] => This is wild[name] => Lingras)php > //The count() gives you the size of an array.

php > echo count($a);7

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Arraysphp > //The "construct" array() (not a function) can be used to create an entire array.

php > $a = array("This", "is", "different", 5, 6, 7);php > print_r($a);Array([0] => This[1] => is[2] => different[3] => 5[4] => 6[5] => 7)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Arrays: Summary• A PHP array index is really just a means of accessing a particular

element in an array. It does not necessarily suggest an ordering. That is why we can use -90 as an index, which is stored in the array after the index 1.

• If we do not use an index to place a value in an array, the first numeric index greater than the last-used numeric index is used.

• Array elements can be displayed using echo, but you need to use the built-in function print_r() to print an entire array.

• A function called count() gives us the size of an array.• An entire array can be created by using the array() construct.• If we want conventionally ordered numeric indices from an

associative array, we can use the function array_values().

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Chapter 10: PHP and MySQL for Client-Server Database Interaction

$_SESSION

• Super global• Array containing session variables available as

a global (that you define the index)• Temporarily store data specific to a particular

user while he/she surfs your website.• data is stored on web server• At the beginning of your script, make a call to

the session_start() function

Logging In to the e-store

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Opening Lines of PHP Code and XHTML Markup for the Login Form

Chapter 10: PHP and MySQL for Client-Server Database Interaction

JavaScript Code for Validating the Login Form Data Entries

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Rest of the XHTML Markup and PHP Code for the Login Form (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Rest of the XHTML Markup and PHP Code for the Login Form (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code:Processing Login Form (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

From the login page

Load the session array

PHP Code:Processing Login Form (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Page it came from

Request For a User to Try Again after a Failed Login

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Passing Key/Value Pairs in a URL$goto = "Location: ../purchase.php?prod=$prod";header('Location: ../login.php?retry=true');

• At the end of each of these lines we see a ? separating a key/value pair in which the key and value are themselves separated by an equals sign.

• The value half of the pair can be provided as a literal value (true) or as a variable ($prod).

• More than one key/value pair can be passed along in this way, in which case an & separates the key/value pairs:login.php?retry=true&otherKey=otherValue

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code forProcessing the Logout (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Destroy the session

PHP Code forProcessing the Logout (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Display Indicating a Successful Logout

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Display When an Attempt is Made to Log Out Without Having Logged In

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

e-store After Successful Login

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Revised banner.php (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Revised banner.php (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Listing of the Departments and Product Categories in Our e-store

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

PHP Code for DisplayingDepartments and Categories (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code for DisplayingDepartments and Categories (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Use a query to display the department

A listing of the Individual Productsin a Category

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

PHP Code for Displaying Productsin a Category (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code for Displaying Productsin a Category (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Building an HTML table from the numRecords result and a loop

Chapter 10: PHP and MySQL for Client-Server Database Interaction

Purchasing a First Product

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Purchasing Additional Products:Trying to Buy More than the Stock

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Try Again: Do not RequestMore than Available

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Viewing the Shopping Cart withTwo Items: Ready for Checkout

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

PHP Code for Main Part of the PHP Script for Processing Purchases (1 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code for Main Part of the PHP Script for Processing Purchases (2 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code for Main Part of the PHP Script for Processing Purchases (3 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Retrieving Product Information for an Order-in-progress

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Creating a New Order

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Displaying the Table Header for the Shopping Cart Display

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Displaying the Table Footer for the Shopping Cart Display

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Displaying the First Four Table Columns for a Product

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Displaying the Last Three Columns for Each Existing Item

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Displaying the Last Three Columns for Each New Item

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code forAdding a New Item to the Cart (1 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code forAdding a New Item to the Cart (2 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code forAdding a New Item to the Cart (3 of 3)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code forDeleting an Item from the Cart

Chapter 10: PHP and MySQL for Client-Server Database Interaction

A Shopping Cart with a Single Item: Ready for Checkout

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Receipt Displayed upon Completion of the Checkout Process

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

Confirming the Reduction in Inventory after Purchase

Chapter 10: PHP and MySQL for Client-Server Database InteractionCourtesy of Nature’s Source.

XHTML and PHP Code for Relevant Parts from payment.php

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Code the Main Part of the Script for Processing a Payment

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function forMarking an Order as Paid (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function forMarking an Order as Paid (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Marking Each Individual Item as Paid (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Marking Each Individual Item as Paid (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Reducing the Inventory Level After Payment (1 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction

PHP Function for Reducing the Inventory Level After Payment (2 of 2)

Chapter 10: PHP and MySQL for Client-Server Database Interaction