Syllabus Schedule Project Labs Canvas Server Access phpMyAdmin Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9

Assignment 8 - Bookstore: Shopping Cart and Checkout

In this assignment you will build the first steps of the checkout process: a) a cookie-based shopping cart, and b) display customer information from the database.

To avoid confusion related to grading and file modification dates it is a good idea to copy all bookstore files to a new folder named something like 'Bookstore2.'


  1. shoppingCart.php -- The shopping cart uses a cookie to store the ISBNs of the books currently in the cart. Here is the code for a basic shopping cart (source). This code manages the information in the cookie but needs to be modified to list the names of the books in the cart and calculate costs.  Steps:
    1. Book titles need to be retrieved from the database. Dynamically build a SQL statement inside a foreach loop that includes the ISBN for each book in the cart. The syntax of the finished SQL statement will look something like this:
      SELECT isbn, title, price
      FROM bookdescriptions
      WHERE isbn='0596005431' OR isbn='0596006810' OR isbn='0596528124'
    2. To calculate shipping costs make up a simple pricing schedule something like: $5 for first item and $3 for each additional item. 
    3. Note on writing cookies: Some students may wish to write the total price, number of items, etc. to a cookie to display in a "shopping cart status box." When writing a cookie after a significant about text has been output you may receive the error ""Cannot modify header information - headers already sent." This problem is easily solved by turning on the output buffer (see ob_start).
  2. checkout01.php -- This page displays a textbox for the customer's email address and the number of items in the cart. Validate the email address using server-side validation. Links to the validation examples discussed in class:  Validation.php, ValidationConfirm.php (source), ValidationUtilities.php.  You will need to copy ValidationUtilities.php to your bookstore folder.
  3. checkout02.php -- This page queries the database for the email address entered in checkout01.php. If the email is found it displays the customer's information in textboxes. If not found it populates only email textbox. Steps:
      1. Create a new table in the database to store customer information. Name it bookCustomers and create the following fields for the customer information:

        Field Type Null Extra
        custID int(11) No auto_increment, primary key
        fname varchar(20) No  
        lname varchar(20) No  
        email varchar(50) No  
        street varchar(25) No  
        city varchar(30) No  
        state varchar(2) No  
        zip varchar(5) No  

      2. You will need some sample customer data for testing your php code. Use phpMyAdmin to add one or two customer records.
      3. The customer greeting changes depending upon whether their email is found in the database. After querying the database the code to check if the user is found looks something like this:

        if (mysqli_num_rows($result) == 0 ) {
        echo "New Customer - Please provide your shipping address.";
        else {
        echo "Returning Customer - Please confirm your mailing and e-mail addresses.";
        $row = mysqli_fetch_array($result);

      4. Use the same HTML form to collect user information for both new and returning customers. To populate the textboxes with database data use the syntax:

        value="<?php echo $row['fname'];?>"

        This will display the value from the database if a customer was found and display a blank text field if not found. The email address entered by the user in checkout01 is always displayed.

  4. When you submit the assignment for grading include an email address that is in the database. This will allow the grader to see if the form preloads the customer information from the database. The data will be written to the database in checkout03.php (assignment 9).

Submission instructions: Submit assignments via the Canvas course management system. Submit the full URL for each exercise in the assignment, listing the URLs in the same order that they are listed in the assignment. To minimize typos in URLs it is strongly recommended that you copy the URLs from the address bar of the browser rather than trying to type them. Incorrect URLs will not be graded and no credit will be given.

When pages are connected via navigation (as in your Bookstore project) it is only necessary to submit the URL of the first page.

Regular Expressions HTML Color Names Color Picker ASCII Character Codes Unicode Character Codes