Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PHP and MySQL for Client-Server Database Interaction Chapter 10."— Presentation transcript:

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

2 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

3 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

4 Current State of Our Website Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source. Inset © Photos.com

5 Bird's Eye View of e-store Functionality Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

6 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

7 Registering for Our Web Site Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source. You can go to the CD and get this page as a starting point, then edit it.

8 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 use Chapter 10: PHP and MySQL for Client-Server Database Interaction

9 XHTML and PHP Code for the Registration Form Chapter 10: PHP and MySQL for Client-Server Database Interaction These are.php files

10 Validating the Form Data (1 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

11 Validating the Form Data (2 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

12 Validating the Form Data (3 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

13 The php files for the customer registration process Chapter 10: PHP and MySQL for Client- Server Database Interaction 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.php Calls the processRegistration script/page and provides a place to return to when the processing is done. Not very big register.php This 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.

14 Messages Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

15 Re-registering for the Site with Different e-mail but Same Username Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

16 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.

17 Main Program from processRegistration.php (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

18 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.

19 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 correct Notice the addition of “ ‘$_POST[…….]’”

20 A Helper Function from processRegistration.php Chapter 10: PHP and MySQL for Client-Server Database Interaction

21 Another Helper Function from processRegistration.php Chapter 10: PHP and MySQL for Client-Server Database Interaction

22 PHP Arrays php > //The following command creates an array: php > $a[] = "Hello"; php > //We omitted the subscript so the next available php > //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; Array php > //But... the echo command can print elements of an array. php > echo $a[0]; Hello

23 PHP Arrays php > //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

24 PHP Arrays php > //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 have php > //expected, since -90 comes after 1, and 70 comes after 90. Chapter 10: PHP and MySQL for Client-Server Database Interaction

25 PHP Arrays php > //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

26 PHP Arrays php > //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

27 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

28

29 $_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 Chapter 10: PHP and MySQL for Client- Server Database Interaction

30 Logging In to the e-store Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

31 Opening Lines of PHP Code and XHTML Markup for the Login Form Chapter 10: PHP and MySQL for Client-Server Database Interaction

32 JavaScript Code for Validating the Login Form Data Entries Chapter 10: PHP and MySQL for Client-Server Database Interaction

33 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

34 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

35 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

36 PHP Code: Processing Login Form (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction Page it came from

37 Request For a User to Try Again after a Failed Login Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

38 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

39 PHP Code for Processing the Logout (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction Destroy the session

40 PHP Code for Processing the Logout (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

41 Display Indicating a Successful Logout Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

42 Display When an Attempt is Made to Log Out Without Having Logged In Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

43 e-store After Successful Login Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

44 Revised banner.php (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

45 Revised banner.php (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

46 Listing of the Departments and Product Categories in Our e-store Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

47 PHP Code for Displaying Departments and Categories (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

48 PHP Code for Displaying Departments and Categories (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction Use a query to display the department

49 A listing of the Individual Products in a Category Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

50 PHP Code for Displaying Products in a Category (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

51 PHP Code for Displaying Products in 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

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

53 Purchasing a First Product Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

54 Purchasing Additional Products: Trying to Buy More than the Stock Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

55 Try Again: Do not Request More than Available Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

56 Viewing the Shopping Cart with Two Items: Ready for Checkout Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

57 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

58 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

59 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

60 PHP Function for Retrieving Product Information for an Order-in-progress Chapter 10: PHP and MySQL for Client-Server Database Interaction

61 PHP Function for Creating a New Order Chapter 10: PHP and MySQL for Client-Server Database Interaction

62 PHP Function for Displaying the Table Header for the Shopping Cart Display Chapter 10: PHP and MySQL for Client-Server Database Interaction

63 PHP Function for Displaying the Table Footer for the Shopping Cart Display Chapter 10: PHP and MySQL for Client-Server Database Interaction

64 PHP Function for Displaying the First Four Table Columns for a Product Chapter 10: PHP and MySQL for Client-Server Database Interaction

65 PHP Function for Displaying the Last Three Columns for Each Existing Item Chapter 10: PHP and MySQL for Client-Server Database Interaction

66 PHP Function for Displaying the Last Three Columns for Each New Item Chapter 10: PHP and MySQL for Client-Server Database Interaction

67 PHP Code for Adding a New Item to the Cart (1 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

68 PHP Code for Adding a New Item to the Cart (2 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

69 PHP Code for Adding a New Item to the Cart (3 of 3) Chapter 10: PHP and MySQL for Client-Server Database Interaction

70 PHP Code for Deleting an Item from the Cart Chapter 10: PHP and MySQL for Client-Server Database Interaction

71 A Shopping Cart with a Single Item: Ready for Checkout Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

72 Receipt Displayed upon Completion of the Checkout Process Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

73 Confirming the Reduction in Inventory after Purchase Chapter 10: PHP and MySQL for Client-Server Database Interaction Courtesy of Nature’s Source.

74 XHTML and PHP Code for Relevant Parts from payment.php Chapter 10: PHP and MySQL for Client-Server Database Interaction

75 PHP Code the Main Part of the Script for Processing a Payment Chapter 10: PHP and MySQL for Client-Server Database Interaction

76 PHP Function for Marking an Order as Paid (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

77 PHP Function for Marking an Order as Paid (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

78 PHP Function for Marking Each Individual Item as Paid (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

79 PHP Function for Marking Each Individual Item as Paid (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

80 PHP Function for Reducing the Inventory Level After Payment (1 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction

81 PHP Function for Reducing the Inventory Level After Payment (2 of 2) Chapter 10: PHP and MySQL for Client-Server Database Interaction


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

Similar presentations


Ads by Google