+ All Categories
Home > Documents > PHP and MySQL for Client-Server Database Interaction Chapter 10.

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

Date post: 11-Jan-2016
Category:
Upload: kristin-stafford
View: 238 times
Download: 2 times
Share this document with a friend
81
PHP and MySQL for Client-Server Database Interaction Chapter 10
Transcript
Page 1: PHP and MySQL for Client-Server Database Interaction Chapter 10.

PHP and MySQL for Client-Server Database Interaction

Chapter 10

Page 2: 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

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

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

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

Current State of Our Website

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

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

Bird's Eye View of e-store Functionality

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

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

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

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

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.

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

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

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

XHTML and PHP Code for the Registration Form

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

These are .php files

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

Validating the Form Data (1 of 3)

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

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

Validating the Form Data (2 of 3)

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

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

Validating the Form Data (3 of 3)

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

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

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.

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

Messages

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

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

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.

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

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.

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

Main Program from processRegistration.php (1 of 2)

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

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

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.

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

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[…….]’”

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

A Helper Function from processRegistration.php

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

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

Another Helper Function from processRegistration.php

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Logging In to the e-store

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

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

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

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

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

JavaScript Code for Validating the Login Form Data Entries

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

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

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

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

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

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

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

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

PHP Code:Processing Login Form (2 of 2)

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

Page it came from

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

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.

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

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

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

PHP Code forProcessing the Logout (1 of 2)

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

Destroy the session

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

PHP Code forProcessing the Logout (2 of 2)

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

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

Display Indicating a Successful Logout

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

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

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.

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

e-store After Successful Login

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

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

Revised banner.php (1 of 2)

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

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

Revised banner.php (2 of 2)

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

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

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.

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

PHP Code for DisplayingDepartments and Categories (1 of 2)

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

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

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

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

A listing of the Individual Productsin a Category

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

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

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

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

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

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

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

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

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

Purchasing a First Product

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

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

Purchasing Additional Products:Trying to Buy More than the Stock

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

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

Try Again: Do not RequestMore than Available

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

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

Viewing the Shopping Cart withTwo Items: Ready for Checkout

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

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

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

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

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

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

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

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

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

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

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

PHP Function for Creating a New Order

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

PHP Code forDeleting an Item from the Cart

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

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

A Shopping Cart with a Single Item: Ready for Checkout

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

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

Receipt Displayed upon Completion of the Checkout Process

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

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

Confirming the Reduction in Inventory after Purchase

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

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

XHTML and PHP Code for Relevant Parts from payment.php

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


Recommended