我们需要找到一种将数据库连接到用户的浏览器的方法。用户能够按目录浏览商品。 用户应该能够从商品目录中选取商品以便此后的购买。我们也要能够记录他们选中的物品。 当用户完成购买,要合计他们的订单,获取运送商品细节,并处理付款。 创建一个管理界面,以便管理员在上面添加、编辑图书和目录。
2、解决方案
2.1 用户视图
2.2 管理员视图
2.3 Book-O-Rama中的文件列表
3、实现数据库3.1 创建book_sc数据库的SQL代码
CREATE DATABASE book_sc; #创建book_sc数据库 USE book_sc; #使用book_sc数据库 CREATE TABLE customers #创建用户表 ( customerid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(60) NOT NULL, address CHAR(80) NOT NULL, city CHAR(30) NOT NULL, state CHAR(10), 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(60) NOT NULL, ship_address CHAR(80) NOT NULL, ship_city CHAR(30) 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(80), title CHAR(100), catid INT UNSIGNED, price FLOAT(4,2) NOT NULL, description VARCHAR(255) ); CREATE TABLE categories #创建目录表 ( catid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, catname CHAR(60) 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(40) NOT NULL ); GRANT SELECT,INSERT,UPDATE,DELETE on book_sc.* to book_sc@localhost IDENTIFIED by 'password';
3.2 数据库测试数据文档
USE book_sc; INSERT INTO books VALUES ('0672329166','Luke Welling and Laura Thomson','PHP and MySQL Web Development',1,49.99, 'PHP & MySQL Web Development teaches the reader to develop dynamic, secure e-commerce web sites. You will learn to integrate and implement these technologies by following real-world examples and working sample projects.'); INSERT INTO books VALUES ('067232976X','Julie Meloni','Sams Teach Yourself PHP, MySQL and Apache All-in-One',1,34.99, 'Using a straightforward, step-by-step approach, each lesson in this book builds on the previous ones, enabling you to learn the essentials of PHP scripting, MySQL databases, and the Apache web server from the ground up.'); INSERT INTO books VALUES ('0672319241','Sterling Hughes and Andrei Zmievski','PHP Developer\'s Cookbook',1,39.99, 'Provides a complete, solutions-oriented guide to the challenges most often faced by PHP developers\r\nWritten specifically for experienced Web developers, the book offers real-world solutions to real-world needs\r\n'); INSERT INTO categories VALUES (1,'Internet'); INSERT INTO categories VALUES (2,'Self-help'); INSERT INTO categories VALUES (5,'Fiction'); INSERT INTO categories VALUES (4,'Gardening'); INSERT INTO admin VALUES ('admin', sha1('admin'));
4、实现在线目录
主页-目录
由以下代码实现:
4.1 index.php
<?php /** * @author switch * @copyright 2015 * 网站首页,显示系统中的图书目录 */ //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once('book_sc_fns.php'); session_start(); //开始会话 do_html_header('Welcome to Book-O-Rama'); //页头 echo "<p>Please choose a category:</p>"; $cat_array = get_categories(); //从数据库获取目录 display_categories($cat_array); //显示目录链接 if(isset($_SESSION['admin_user'])) //如果是管理员,显示管理员操作 display_button("admin.php","admin-menu","Admin Menu"); do_html_footer(); //页尾 ?>
4.2 book_fns.php文件中的函数get_categories()
function get_categories() //从数据库中获取目录列表 { $conn = db_connect(); //连接数据库 $query = "select catid,catname from categories"; $result = @$conn ->query($query); if(!$result) //查询失败,返回false return false; $num_cats = @$result ->num_rows; if($num_cats == 0) //数据库中无目录,返回false return false; $result = db_result_to_array($result); return $result; }
4.3 output_fns.php文件中的函数display_categories()