这是我在研究生期间,老师要求做的一个类似原始淘宝网的购物网站,
因为不会PHP,为了写这个作业而新学的。
做这个网站用了两周时间,在此把这个小项目做一个总结。
这个小项目做的时间非常赶,一共两周,实际有效时间只有10天,中间还在忙其他的事。所以有很多不足之处。
有些代码原本可以写的更精简,合并在一起。
连接数据库和其他的一些执行sql语句的操作,可以封装在单独的文件里面调用,这里也都比较简单的哪里用到就在哪里处理了。
数据库的链接方式用了两种,没有统一。
还是有很多值得改进的地方。
DONE. 用户权限管理
。包括管理员和普通用户。管理员有所有权限,包括更新网站状态
登录的其他值为用户名和密码,用户名和密码正确,跳转到下一页。
ADMIN具有添加,删除,更新等权限。用户只能查看手机,只能添加到购物车中的手机等。
DONE.新用户
:此模块适用于没有帐户的用户。这里用户可以创建一个帐户来登录。账户的创建是通过填写注册表单和用户的详细信息,如姓名,电话,电子邮件等来完成的。
DONE.产品管理和展示
:该模块展示手机产品信息,如产品编号,项目,名称,类别,产品图像,说明,功能和产品限制等。所有这些都将输入到数据库,因此可以在网站上找到。
DONE.搜索
:该模块可帮助客户根据自己的预算或兴趣来放松搜索。搜索可以在不同的类别上完成,如品牌,型号名称,型号,颜色或价格等。
DONE:交易
:在此模块中,购物车的管理已完成。此模块购物者可以选择任意数量的物品(手机,配件)并将它们添加到购物车,在从购物车购买物品后,所有要购买的物品都可以再次查看。如果他以后不喜欢,购物者也可以从购物车中取出。购物者还可以逐一检查购物车中保存的产品。由于产品从购物车中检出,总价格将相加。
DONE:装运
:在这个模块中,购物者可以选择合适的装运选项。购物者可以使用不同服务提供商提供的各种运送选项。
DONE:付款
:该模块描述了客户完成的付款。购物者可以选择不同的支付方式,并根据所选的支付方式的要求提供机密的支付信息。付款信息还可能包括购买型号,数量和供应商名称等信息。
DONE:报告
:在此模块中,将生成所有报告。无论何时出售物品或客户订购产品,都应立即通过电子邮件向其供应商发送警报,以便他可以尽快装运该物品。该模块有3个子模块;股票报告,订单报告和交付报告。
###1.数据库表设计
下面是对数据库设计的一个说明,交作业用的。
Design ideas of relational schema:
Since the website will not be too complex, so I just design basic fields of the whole website logic. As for the tables “order_info” and “orderDetailRecord_info”, I separate order information into two tables to solve the problem that one order may have two types of products. Plus, the “p_image_url” field in the table “product_info”, will be used for analyzing url of images of products. Plus plus: actually an order may conclude many products, every products may choose different delivery method, but here we simplify it and assume that an order only have one delivery method.
另外需要说明的是,
delivery_info
表和payment_info
表中分别加入了一个random
字段,是因为在写php处理的过程中,产生了相应的需要,具体处理请看代码。
下面是具体的表名和字段:
附上刚开始创建数据表的mysql代码,后期自此基础上有一些修改
`admin_info(admin_id,admin_name,admin_pwd)`
CREATE TABLE IF NOT EXISTS admin_info(
admin_id int(10) NOT NULL AUTO_INCREMENT,
admin_name varchar(50) NOT NULL,
admin_pwd varchar(50) NOT NULL,
PRIMARY KEY(admin_id)
);
`user_info(u_id,u_name,u_pwd,u_phone,u_email)`
CREATE TABLE IF NOT EXISTS user_info(
u_id int(10) NOT NULL AUTO_INCREMENT,
u_name varchar(50) NOT NULL,
u_pwd varchar(50) NOT NULL,
u_phone int(50) NOT NULL,
u_email varchar(50) NOT NULL,
PRIMARY KEY(u_id)
);
`product_info(p_id, p_name,p_brand, p_type,p_price,p_inventory,p_descr,p_color,p_image_url)`
CREATE TABLE IF NOT EXISTS product_info(
p_id int(10) NOT NULL AUTO_INCREMENT,
p_name varchar(50) NOT NULL,
p_brand varchar(50) NOT NULL,
p_type int(50) NOT NULL,
p_price varchar(50) NOT NULL,
p_inventory varchar(50) NOT NULL,
p_descr varchar(100) NOT NULL,
p_color varchar(50) NOT NULL,
p_image_url varchar(200) NOT NULL,
PRIMARY KEY(p_id)
);
`payment_info(pay_id,pay_user,receive_user,pay_account,receive_account,pay_status)`
CREATE TABLE IF NOT EXISTS payment_info(
pay_id int(10) NOT NULL AUTO_INCREMENT,
pay_user varchar(50) NOT NULL,
receive_user varchar(50) NOT NULL,
pay_account int(50) NOT NULL,
receive_account int(50) NOT NULL,
pay_status boolean NOT NULL,
PRIMARY KEY(pay_id)
);
`delivery_info(d_id,d_company,d_init_add,d_trgt_add,d_price)`
CREATE TABLE IF NOT EXISTS delivery_info(
d_id int(10) NOT NULL AUTO_INCREMENT,
d_company varchar(50) NOT NULL,
d_init_add varchar(50) NOT NULL,
d_trgt_add varchar(50) NOT NULL,
d_price varchar(50) NOT NULL,
PRIMARY KEY(d_id)
);
`order_info(o_id,u_id,d_id,o_date,pay_id)`
CREATE TABLE IF NOT EXISTS order_info(
o_id int(10) NOT NULL AUTO_INCREMENT,
u_id int NOT NULL,
d_id int NOT NULL,
o_date int(50) NOT NULL,
pay_id int NOT NULL,
PRIMARY KEY(o_id),
FOREIGN KEY (u_id) REFERENCES user_info(u_id),
FOREIGN KEY (d_id) REFERENCES delivery_info(d_id),
FOREIGN KEY (pay_id) REFERENCES payment_info(pay_id)
);
`orderDetailRecord_info(r_id,o_id,p_id,p_num)`
CREATE TABLE IF NOT EXISTS orderDetailRecord_info(
r_id int(10) NOT NULL AUTO_INCREMENT,
o_id int NOT NULL,
p_id int NOT NULL,
p_num int(50) NOT NULL,
PRIMARY KEY(r_id),
FOREIGN KEY (o_id) REFERENCES order_info(o_id),
FOREIGN KEY (p_id) REFERENCES product_info(p_id)
);
`stock_info(s_id,p_id,p_inventory)`
CREATE TABLE IF NOT EXISTS stock_info(
s_id int(10) NOT NULL AUTO_INCREMENT,
p_id int(10),
p_inventory int(50),
PRIMARY KEY(s_id),
FOREIGN KEY(p_id) REFERENCES product_info(p_id)
);
下面是以上功能的代码以及一些相关的解释:
最简单的主界面:
index.html
//只放了一个注册一个登录的链接
Phones on saling
Phones on saling!
Sign in the website.
Login into the website.
###1.注册功能:
首先要选择角色类型:(这个功能是刚开始练习做的,其实应该把角色选择和注册功能都放在一个页面里,现在分为了三个,分别是选择,管理员注册和用户注册,比较麻烦。后来时间紧,就没有再改了,实际可以合并为一个。)
chooseCharacter.html
Sign in to phone website
Choose your charactor
Please choose which kind of charactor you want to sign in?
chooseCharacter.php
//选择相应的角色后,会跳转到不同角色的注册界面
Choose charactor
1)管理员注册:
adminSign.html
//管理员注册的界面。管理员注册需要拿到内部的Invitation number(邀请码),注册方可进行。如果已经注册,可以点击下方的login链接,直接登录,将跳转到login.php界面。
Sign in to phone website as admin user
Sign in
If you have already signed in, please click here to login.
Login into the website.
adminSign.php
//处理管理员注册请求
Sign in the phone web as admin user, success!
2)用户注册
sign.html
//用户注册界面
Sign in to phone website
Sign in
sign.php
//处理用户注册请求,收集基本信息并加入到数据库。如果存在数据缺失,则不能注册,对两次输入的密码做了基本的检测,并检测邮箱格式的正确性。
//注册后会跳转到login.php界面,但是因为普通用户注册后,会自动为当前用户登录,并在当前的cookie中存储用户登录的状态,因此不需要再登录一次,可以直接由网页链接跳转到手机购买界面。
Sign in sucess!
###2.登录功能:
login.php
//用户登录的界面,可以选择管理员用户登录和普通用户登录。
作为管理员用户登录后,跳转到产品管理界面。作为普通用户登录后,跳转到网站主页,即手机购买界面。
Login in to phone website
User Login
Click here to buy phones.
process_login.php
处理登录请求
";
}
if($userName == ""||$pwd == ""){
echo "None of the value can be empty!";
echo "
";
}
//declare the sql var and decides the value
//$sql;
if($selected_Charactor == "admin"){
$sql = "SELECT admin_id FROM admin_info WHERE admin_name = '" . $userName . "' and admin_pwd = '". $pwd ." ' ;" ;
$result = executeSql($sql);
if ($result[0]) {
header('Location: p_manage.php');
} else {
echo "Error! Something wrong in your username or password!";
echo "
";
}
}else if($selected_Charactor == "user"){
$sql = "SELECT u_id FROM user_info WHERE u_name = '" . $userName ."' and u_pwd = '".$pwd."' ;" ;
$result = executeSql($sql);
if($result[0]){
setcookie('login_status',true);
while ($row = mysqli_fetch_assoc($result[1])){
$u_id=$row["u_id"];
setcookie('u_id',$u_id);
}
header('Location: showPhones.php');
}else{
echo "Error! Something wrong in your username or password!";
echo "
";
}
}
?>
###3.手机产品管理(管理员):
1)增加新的手机:
add_product.html
//增加新的手机库存
Add new product
Hello admin user, you can add a new product into database!
add_product.php
//处理增加请求
Add new product
";
}else{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hw";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("Fail to connect!: " . $conn->connect_error);
}
//执行sql语句
if ($conn->query($sql) === TRUE) {
$flag = TRUE;
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
$conn->close();
return $flag;
}
}
$p_name=$_POST["name"];
$p_brand=$_POST["brand"];
$p_type=$_POST["type"];
$p_price=$_POST["price"];
$p_inventory=$_POST["inventory"];
$p_descr=$_POST["descr"];
$p_color=$_POST["color"];
$p_image_url=$_POST["url"];
if($p_name ==""||$p_brand ==""||$p_type ==""||$p_price ==""||$p_inventory ==""||$p_descr ==""||$p_color ==""){
echo "You can not provide empty values!";
}else{
$sql = "INSERT INTO product_info(p_name,p_brand,p_type,p_price,p_descr,p_color,p_image_url) VALUES
('".$p_name."','".$p_brand."','".$p_type."','".$p_price."','".$p_descr."','".$p_color."','".$p_image_url."');";
$result = executeSql($sql);
if($result){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hw";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_sql = "SELECT * FROM product_info WHERE p_name = '".$p_name."';";
$result=mysqli_query($conn,$select_sql);//result is a PHP array
var_dump($result);
$num_rows=mysqli_num_rows($result);
//echo $num_rows;
mysqli_close($conn);
while ($row = mysqli_fetch_assoc($result)){
$p_id=$row["p_id"];
$insert_sql = "INSERT INTO stock_info(p_id,p_inventory) VALUES (".$p_id.",".$p_inventory.");";
$feedback = executeSql($insert_sql);
if($feedback){
header("location:p_manage.php");
}
}
}
}
?>
2)管理员管理手机(查看,删除,etc)
p_manage.php
Read product information from database
Welcome! Admin user. This is the page of Product Management.
Product ID
Product Name
Product Brand
Product Type
Product Price
Product Inventory
Product Description
Product Color
Product Image
Delete Product
";
echo "".$p_id." ";
echo "".$p_name." ";
echo "".$p_brand." ";
echo "".$p_type." ";
echo "".$p_price." ";
echo "".$p_inventory." ";
echo "".$p_descr." ";
echo "".$p_color." ";
//$image = 'https://cdn2.gsmarena.com/vv/pics/apple/apple-iphone-x-new-1.jpg';
$imageData = base64_encode(file_get_contents($p_image_url));
//var_dump($imageData);
//echo '';
echo '[外链图片转存失败(img-WDPS9q7a-1562059529495)(data:image/jpeg;base64,'.$imageData.')] ';
//echo '';
//echo " ";
?>
Delete
";
$i++;
}
mysqli_close($conn);
?>
界面如图所示(缩小版的界面)
手机展示界面,并可实现增加产品到购物车,没有实现批量添加,每点击一次手机产品对应的添加按钮,则购物车中增加一条该产品的记录。
添加后会在购物车功能模块处理,如果已经添加够了,也可以直接点击页面最下方的链接,查看购物车。
showPhones.php
//代码和p_manage.php类似,有些功能类似或重合
Product information
Welcome! You can buy your own phone here.
Product Name
Product Brand
Product Type
Product Price
Product Inventory
Product Description
Product Color
Product Image
Add to Cart
";
echo "".$p_name." ";
echo "".$p_brand." ";
echo "".$p_type." ";
echo "".$p_price." ";
echo "".$p_inventory." ";
echo "".$p_descr." ";
echo "".$p_color." ";
//$image = 'https://cdn2.gsmarena.com/vv/pics/apple/apple-iphone-x-new-1.jpg';
$imageData = base64_encode(file_get_contents($p_image_url));
//var_dump($imageData);
echo '[外链图片转存失败(img-ePhuvnsp-1562059529496)(data:image/jpeg;base64,'.$imageData.')] ';
?>
addCart
";
$i++;
}
mysqli_close($conn);
?>
Enough adding, click here to shopcart.
###5.购物车
1)process_shopCart.php
//处理添加请求
All fees of shipment.
2)view_shopCart.php
//查看购物车
Shop cart
View your shop cart here.
Product Name
Product Brand
Product Price
Product Description
Product Color
Counts
Delete from Cart
";
echo "".$p_name." ";
echo "".$p_brand." ";
echo "".$p_price."HKD ";
echo "".$p_descr." ";
echo "".$p_color." ";
echo "".$goods_num." ";
?>
Delete
";
$singlePrice = $p_price * $goods_num;
$totalPrice = $totalPrice + $singlePrice;
$totalItem = $totalItem + $goods_num;
setcookie("total_item",$totalItem);
setcookie("phones_price",$totalPrice);
}
}
//echo $p_info;
setcookie('p_info',$p_info);
?>
Clear cart
Shipment
Back to add goods
购物车如下图:
购物车中会展示所有产品的信息,并计算他们的总价格。
3)delCart.php
//如果用户在查看购物车时点击删除某项产品,将该产品从购物车中全部删除
4)clearCart.php
//如果用户在查看购物车时,点击了清空购物车,将当前购物车中内容全部清空
";
echo "
";
$result = session_destroy();
}else{
echo "There is no goods in shop cart!";
}
echo "
";
echo $result;
echo "
";
echo "
";
var_dump($_SESSION);
header("location:view_shopCart.php");
?>
###6.物流
点击购物车中的’shipment’,选择装运物流信息。
shipment.php
Shipment
You have already fill the shipment information
Click here to pay
Choose your shipment way
process_shipment.php
//处理物流信息请求
All fees of shipment.
物流选择界面如图:
###7.支付
1)payInfo.php
//计算商品和物流的总价格并展示,让用户选择支付方式。如果已经选择了支付方式(检查cookie中的值),提升已经选择,并且给出跳转动支付页面的链接。否则让用户选择支付方式,提供了四种,微信,支付宝,信用卡和中国银联,默认选项为支付宝
Shop cart
Total money here, please fill your payment information.
Click here to continue
Total Item
Phones Price
Shipment Way
Shipment Price
Total Price
";
echo "".$total_item." ";
echo "".$phonesPrice." ";
echo "".$shipment_way." ";
echo "".$shipment_price." ";
echo "".$totalPrice." ";
echo "";
?>
界面如图:
2)pay_way.php
//将用户支付信息填入数据库表中,并跳转到pay_money.php
3)pay_money.php
//根据payInfo.php中选择的支付方式,打开相应的界面,让用户登录并付钱。
然后将订单信息全部丢给process_order.php处理
//这里有一点需要特别说明的是,因为这是一个练习,数据都是虚拟的,所以无法从支付宝或者微信,银联等获知用户支付已经支付成功,所以这里将是否已经支付的判定设置为,只要用户填写了付款信息,并点击付款,打开了支付页面,这里就在cookie中设置为已支付状态
window.open('https://auth.alipay.com/login/index.htm?goto=https%3A%2F%2Fmy.alipay.com%2Fportal%2Fi.htm')";
//$image_url = "https://www.hkelectric.com/zh/CustomerServices/PublishingImages/Alipay_Download_QR.jpg";
//$imageData = base64_encode(file_get_contents($image_url));
//echo '[外链图片转存失败(img-0UVbanjU-1562059529497)(data:image/jpeg;base64,'.$imageData.')]';
}else if($payWay == "WeChatPay"){
//$image_url = "https://3.bp.blogspot.com/-ymZs4Aij_f8/WnXUq9v5Z9I/AAAAAAAAFeA/Zrnru65sDLEgGbVbJ_KevD9_izoL3YO5wCLcBGAs/s1600/wechat.jpg";
//$imageData = base64_encode(file_get_contents($image_url));
//var_dump($imageData);
//echo '[外链图片转存失败(img-Ji9HZKJS-1562059529497)(data:image/jpeg;base64,'.$imageData.')]';
echo "";
}else if($payWay == "Credit"){
echo "";
}else if($payWay == "UnionPay"){
echo "";
}
setcookie('pay_status',true);
$sql = "UPDATE payment_info SET pay_status=1 WHERE pay_id = ".$_COOKIE['pay_id'].";";
$result = executeSql($sql);
if($result[0]){
echo "
";
echo "
";
echo "Click here to see order information.";
}else{
echo "You have to pay first!";
}
?>
###8.查看交易信息并导出报告
1)process_order.php
//将订单的信息填入到数据库表中
Order information
2)view_order.php
//查看订单信息,并给出生成报告的链接
Order Information
Order information
Order id
User
Tracking Number
Product Price
Delivery Price
Total Items
Total Price
Payment ID
Pay Status
";
echo "".$o_id." ";
echo "".$u_id." ";
echo "".$tracking_num." ";
echo "".$phones_price."HKD ";
echo "".$shipment_price."HKD ";
echo "".$total_item." ";
echo "".$total_price."HKD ";
echo "".$pay_id." ";
if($pay_status){
echo "Paid ";
}else{
echo "Not Paid ";
}
echo "";
echo "
";
echo "
";
echo "Export Product Report";
echo "
";
echo "
";
echo "Export Order Report";
echo "
";
echo "
";
echo "Export Delivery Report";
}else{
header('location:payInfo.php');
}
?>
1)eOrderReport.php
//导出订单报告
Export Report
2)eStockReport.php
//导出库存报告
Export Report
Host = "ssl://smtp.gmail.com";
$mail = new PHPMailer(); //实例化
$mail->IsSMTP(); // 启用SMTP
//$mail->Host = "smtp.163.com"; //SMTP服务器 163邮箱例子
$mail->Host = "smtp.126.com"; //SMTP服务器 126邮箱例子
//$mail->Host = "smtp.qq.com"; //SMTP服务器 qq邮箱例子
$mail->Port = 25; //邮件发送端口
$mail->SMTPAuth = true; //启用SMTP认证
$mail->CharSet = "UTF-8"; //字符集
$mail->Encoding = "base64"; //编码方式
$mail->Username = ""; //你的邮箱
$mail->Password = ""; //你的密码
$mail->Subject = "Product information updating"; //邮件标题
$mail->From = ""; //发件人地址(也就是你的邮箱)
$mail->FromName = ""; //发件人姓名
$address = "";//收件人email
$mail->AddAddress($address, ""); //添加收件人1(地址,昵称)
//$mail->AddAttachment('xx.xls','我的附件.xls'); // 添加附件,并指定名称
$mail->IsHTML(true); //支持html格式内容
//$mail->AddEmbeddedImage("logo.jpg", "my-attach", "logo.jpg"); //设置邮件中的图片
$mail->Body = $file_stream; //邮件主体内容
//发送
if(!$mail->Send()){
echo "Fialed to send " . $mail->ErrorInfo;
} else {
echo "Successfully send the email!";
}
}
php_sendmail($file_stream);
header('location:view_order.php');
?>
3)eDeliveryReport.php
//导出物流报告
Export Report
###10.搜索功能
做了一个分类搜索的功能,用的就是数据库的模糊查询,很简单
1)search.html
//页面
Search phones
Search what you want
2)search.php
//处理查询请求
";
}
if($selected_Condition == "brand"){
$value = $_POST["value"];
if($value ==""){
echo "The value can't be empty!";
echo "
";
}else{
$sql = "SELECT * FROM product_info WHERE p_brand LIKE '%".$value."%';";
showResult($sql);
}
}else if($selected_Condition == "name"){
$value = $_POST["value"];
if($value ==""){
echo "The value can't be empty!";
echo "
";
}else{
$sql = "SELECT * FROM product_info WHERE p_name LIKE '%".$value."%';";
showResult($sql);
}
}else if($selected_Condition == "type"){
$value = $_POST["value"];
if($value ==""){
echo "The value can't be empty!";
echo "
";
}else{
$sql = "SELECT * FROM product_info WHERE p_type LIKE '%".$value."%';";
showResult($sql);
}
}else if($selected_Condition == "color"){
$value = $_POST["value"];
if($value ==""){
echo "The value can't be empty!";
echo "
";
}else{
$sql = "SELECT * FROM product_info WHERE p_color LIKE '%".$value."%';";
showResult($sql);
}
}else if($selected_Condition == "price"){
$low_range = $_POST["low_range"];
$high_range = $_POST["high_range"];
if($low_range ==""||$high_range == ""){
echo "The range can't be empty!";
echo "
";
}else{
$sql = "SELECT * FROM product_info WHERE p_price BETWEEN ".$low_range." AND ".$high_range.";";
showResult($sql);
}
}
function showResult($sql){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hw";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result=mysqli_query($conn,$sql);//result is a PHP array
$num_rows=mysqli_num_rows($result);
mysqli_close($conn);
if($num_rows == 0){
echo "There is no meeted results.";
}else{
echo '';
echo "";
echo "Product Name ";
echo "Product Brand ";
echo "Product Type ";
echo "Product Price ";
echo "Product Description ";
echo "Product Color ";
echo "Product Image ";
echo " ";
$i=0;
while ($row = mysqli_fetch_assoc($result)){
$p_name=$row["p_name"];
$p_brand=$row["p_brand"];
$p_type=$row["p_type"];
$p_price=$row["p_price"];
$p_descr=$row["p_descr"];
$p_color=$row["p_color"];
$p_image_url = $row["p_image_url"];
echo "";
echo "".$p_name." ";
echo "".$p_brand." ";
echo "".$p_type." ";
echo "".$p_price." ";
echo "".$p_descr." ";
echo "".$p_color." ";
$imageData = base64_encode(file_get_contents($p_image_url));
echo '[外链图片转存失败(img-CAzDYQv5-1562059529498)(data:image/jpeg;base64,'.$imageData.')] ';
echo " ";
$i++;
}
echo "
";
}
}
?>
####特别说明:
1)购物车用session
实现
2)其他各种用户登录状态,产品id等信息,均存储在cookie
数组中
3)当某种产品卖出后,会从数据库中将该产品的库存减去订单中相应的数量。