1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P110 doc

5 96 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

  • Part II: Using MySQL

    • Chapter 7: Designing Your Web Database

    • Chapter 8: Creating Your Web Database

    • Chapter 9: Working with Your MySQL Database

    • Chapter 10: Accessing Your MySQL Database from the Web with PHP

    • Chapter 11: Advanced MySQL

  • Part III: E-commerce and Security

    • Chapter 12: Running an E-commerce Site

    • Chapter 13: E-commerce Security Issues

    • Chapter 14: Implementing Authentication with PHP and MySQL

    • Chapter 15: Implementing Secure Transactions with PHP and MySQL

  • Part IV: Advanced PHP Techniques

    • Chapter 16: Interacting with the File System and the Server

    • Chapter 17: Using Network and Protocol Functions

    • Chapter 18: Managing the Date and Time

    • Chapter 19: Generating Images

    • Chapter 20: Using Session Control in PHP

    • Chapter 21: Other Useful Features

  • Part V: Building Practical PHP and MySQL Projects

    • Chapter 22: Using PHP and MySQL for Large Projects

    • Chapter 23: Debugging

    • Chapter 24: Building User Authentication and Personalization

    • Chapter 25: Building a Shopping Cart

    • Chapter 26: Building a Content Management System

    • Chapter 27: Building a Web-Based Email Service

    • Chapter 28: Building a Mailing List Manager

    • Chapter 29: Building Web Forums

    • Chapter 30: Generating Personalized Documents in Portable Document Format (PDF)

    • Chapter 31: Connecting to Web Services with XML and SOAP

  • Part VI: Appendixes

    • Appendix A: Installing PHP and MySQL

    • Appendix B: Web Resources

  • Index

  • What’s On the CD-ROM?

Nội dung

517 Implementing the Database Implementing the Database As we mentioned earlier, we have made some minor modifications to the Book-O- Rama database presented in Part II. The SQL to create the book_sc database is shown in Listing 25.1. Listing 25.1 book_sc.sql—SQL to Create the book_sc Database create database book_sc; use book_sc; create table customers ( customerid int unsigned not null auto_increment primary key, name char(40) not null, address char(40) not null, city char(20) not null, state char(20), zip char(10), country char(20) not null ); create table orders ( orderid int unsigned not null auto_increment primary key, customerid int unsigned not null, amount float(6,2), date date not null, order_status char(10), ship_name char(40) not null, ship_address char(40) not null, ship_city char(20) not null, ship_state char(20), ship_zip char(10), ship_country char(20) not null ); create table books ( isbn char(13) not null primary key, author char(30), title char(60), catid int unsigned, price float(4,2) not null, description varchar(255) ); 31 525x ch25 1/24/03 3:39 PM Page 517 518 Chapter 25 Building a Shopping Cart create table categories ( catid int unsigned not null auto_increment primary key, catname char(40) not null ); create table order_items ( orderid int unsigned not null, isbn char(13) not null, item_price float(4,2) not null, quantity tinyint unsigned not null, primary key (orderid, isbn) ); create table admin ( username char(16) not null primary key, password char(16) not null ); grant select, insert, update, delete on book_sc.* to book_sc@localhost identified by 'password'; Although nothing was wrong with the original Book-O-Rama interface, we have a few other requirements now that we are going to make it available online. The changes we have made to the original database are as follows: n The addition of more address fields for customers—this is more important now that we are building a more realistic application. n The addition of a shipping address to an order. A customer’s contact address might not be the same as the shipping address, particularly if she is using the site to buy a gift. n The addition of a categories table and a catid to books table. Sorting books into categories will make the site easier to browse. n The addition of item_price to the order_items table to recognize the fact that an item’s price might change.We want to know how much it cost when the customer ordered it. n The addition of an admin table to store administrator login and password details. n The removal of the reviews table—you could add reviews as an extension to this project. Instead, each book has a description field which will contain a brief blurb about the book. Listing 25.1 Continued 31 525x ch25 1/24/03 3:39 PM Page 518 519 Implementing the Online Catalog To set this database up on your system, run the book_sc.sql script through MySQL as the root user, as follows: mysql -u root -p < book_sc.sql (You will need to supply your root password.) Beforehand, you should change the password for the book_sc user to something bet- ter than 'password'. Note that if you change the password in book_sc.sql you will also need to change it in db_fns.php. (You’ll see where in a minute.) We have also included a file of sample data.This is called populate.sql.You can put the sample data into the database by running it through MySQL in this same way. Implementing the Online Catalog Three catalog scripts are in this application: the main page, the category page, and the book details page. The front page of the site is produced by the script called index.php.The output of this script is shown in Figure 25.3. Figure 25.3 The front page of the site lists the categories of books available for purchase. You’ll notice that, in addition to the list of categories on the site, there is a link to the shopping cart in the top-right corner of the screen and some summary information about what’s in the cart.This will appear on every page while a user browses and shops. 31 525x ch25 1/24/03 3:39 PM Page 519 520 Chapter 25 Building a Shopping Cart Figure 25.4 Each book in the category is listed with a photo. All the books in the Internet category are listed as links. If a user clicks one of these links, she will be taken to the book details page.The book details page for one book is shown in Figure 25.5. On this page, as well as the View Cart link, we have an Add to Cart link in which the user can select an item.We’ll return to that when we look at how to build the shopping cart later. Let’s look at each of these three scripts. Listing Categories The first script, index.php, lists all the categories in the database. It is shown in Listing 25.2. If a user clicks one of the categories, she’ll be taken to the category page, produced by the script show_cat.php.The category page for the Internet books section is shown in Figure 25.4. 31 525x ch25 1/24/03 3:39 PM Page 520 521 Implementing the Online Catalog Figure 25.5 Each book has a details page that shows more information including a long description. Listing 25.2 index.php—Script to Produce the Front Page of the Site <?php include ('book_sc_fns.php'); // The shopping cart needs sessions, so start one session_start(); do_html_header('Welcome to Book-O-Rama'); echo '<p>Please choose a category:</p>'; // get categories out of database $cat_array = get_categories(); // display as links to cat pages display_categories($cat_array); // if logged in as admin, show add, delete, edit cat links if(isset($HTTP_SESSION_VARS['admin_user'])) { display_button('admin.php', 'admin-menu', 'Admin Menu'); } 31 525x ch25 1/24/03 3:39 PM Page 521 . run the book_sc.sql script through MySQL as the root user, as follows: mysql -u root -p < book_sc.sql (You will need to supply your root password.) Beforehand, you should change the password. the shopping cart in the top-right corner of the screen and some summary information about what’s in the cart.This will appear on every page while a user browses and shops. 31 525x ch25 1/24/03. information including a long description. Listing 25.2 index .php Script to Produce the Front Page of the Site < ?php include ('book_sc_fns .php& apos;); // The shopping cart needs sessions, so start

Ngày đăng: 07/07/2014, 03:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN