一、登陆页面
封装BDDA
<?php
class DBDA{
public $host="localhost"; //服务器地址
public $uid="root"; //用户名
public $pwd="123"; //密码
public $dbname="shopping"; //数据库名称
/*
执行一条SQL语句的方法
@param sql 要执行的SQL语句
@param type SQL语句的类型,0代表查询 1代表增删改
@return 如果是查询语句返回二维数组,如果是增删改返回true或false
*/
public function query($sql,$type=0){
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql);
if($type){
return $result;
}else{
return $result->fetch_all();
}
}
public function strquery($sql,$type=0){
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql);
if($type){
return $result;
}else{
$arr = $result->fetch_all();
$str = "";
foreach($arr as $v){
$str .= implode("^",$v)."|";
}
$str = substr($str,0,strlen($str)-1);
return $str;
}
}
//返回json数据的方法
public function jsonquery($sql,$type=0){
$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
$result = $db->query($sql);
if($type){
return $result;
}else{
$arr = $result->fetch_all(MYSQLI_ASSOC);//关联数组
return json_encode($arr);//转换json
//json_decode()分解json
}
}
}
css样式表
*{
margin:0px auto;
padding:0px;
}
#login{
background-color:#0CF;
width:30%;
height:200px;
margin-top:50px;
border:5px double #060;
}
#dl{
color:#00F;
background-color:#9FF;
height:30px;
text-indent:10px;
vertical-align:bottom;
line-height:30px;
}
#pwd{
background-color:#CFF;
height:70px;
}
#sb{
background-color:#060;
width:200px;
height:30px;
color:#CFF;
margin-top:3px;
}
布局样式
<body>
<form method="post" action="dengluchuli.php">
<div id="login">
<div align="left" id="dl">登录页面</div><br />
<div id="yhm" align="center">用户名:
<input type="text" placeholder="请输入用户名" name="uid"/></div><br />
<div id="pwd" align="center">密 码:
<input type="password" placeholder="请输入密码" name="pwd"/></div>
<div align="center"><input type="submit" value="登录" id="sb"/></div>
</div>
</form>
</body>
二、登录处理
<?php
session_start();
require_once "../DBDA.class.php";
$db = new DBDA();
$uid = $_POST["uid"];
$pwd = $_POST["pwd"];
$sql = "select password from login where username='{$uid}'";
$mm = $db->strquery($sql);
if(!empty($pwd) && $pwd==$mm){
$_SESSION["uid"] = $uid;
header("location:main.php");
}else{
header("location:denglu.php");
}
三、主页的css样式
*{
margin:0px auto;
padding:0px;
}
#shang{
background-color:#0FF;
width:100%;
height:60px;
vertical-align:bottom;
line-height:90px;
text-indent:10px;
}
#zuo{
float:left;
background-color:#999;
width:30%;
height:500px;
}
.caidan{
width:80%;
height:60px;
background-color:#69F;
margin-top:20px;
text-align:center;
vertical-align:middle;
line-height:60px;
font-weight:bold;
}
#yous{
height:40px;
width:100%;
background-color:#FFF;
vertical-align:bottom;
line-height:60px;
text-indent:10px;
color:#009;
font-size:20px;
}
#you{
background-color:#06F;
width:70%;
height:500px;
float:left;
}
table{
color:#CF0;
text-align:center;
}
主页布局
<body>
<?php
session_start();
if(empty($_SESSION["uid"])){
header("location:denglu.php");
exit;
}
?>
<div>
<div id="shang"><h2>超牛逼购物网</h2></div>
<div>
<div id="zuo">
<div class="caidan"><a href="main.php">浏览商品</a></div>
<div class="caidan"><a href="zhanghu.php">查看账户</a></div>
<div class="caidan"><a href="gouwuche.php">查看购物车</a></div>
</div>
<div id="you">
<div id="yous">
<?php
require_once"../DBDA.class.php";
$db = new DBDA();
if(!empty($_SESSION["gwc"])){
$arr = $_SESSION["gwc"];
$count = count($arr);//购物车中商品的数量
$sum = 0;//商品总价
foreach($arr as $v){
$sql = "select price from fruit where ids ='{$v[0]}'";
$danjia = $db->strquery($sql);
$sum +=$danjia*$v[1];
}
echo "购物车中总共有{$count}种商品,总价为:{$sum}元";
}
?>
</div>
<table border="1" bordercolor="#CCFFFF" width="100%">
<tr bgcolor="#339999" height="40">
<td>水果代号</td>
<td>水果名称</td>
<td>水果价格</td>
<td>水果产地</td>
<td>货架</td>
<td>库存量</td>
<td>操作</td>
</tr>
<?php
$sql = "select * from fruit";
$arr = $db->query($sql);
foreach($arr as $v){
echo"<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[3]}</td>
<td>{$v[4]}</td>
<td>{$v[5]}</td>
<td><a href='tianjia.php?code={$v[0]}'><input type='button' value='添加购物车'/></a></td>
</tr>";
}
?>
</table>
</div>
</div>
</div>
</body>
账号信息
<?php
session_start();
if(empty($_SESSION["uid"])){
header("location:login.php");
exit;
}
?>
<body>
<div>
<div id="shang"><h2>账户信息</h2></div>
<div>
<div id="zuo">
<div class="caidan"><a href="main.php">浏览商品</a></div>
<div class="caidan"><a href="zhanghu.php">查看账户</a></div>
<div class="caidan"><a href="gouwuche.php">查看购物车</a></div>
</div>
<div id="you">
<div id="yous">账户基本信息</div>
<table border="1" bordercolor="#CCFFFF" width="100%">
<tr bgcolor="#339999" height="40">
<td>用户名</td>
<td>姓名</td>
<td>账户余额</td>
</tr>
<?php
$uid = $_SESSION["uid"];
require_once "../DBDA.class.php";
$db = new DBDA();
$sql = "select * from login where username = '{$uid}'";
$arr = $db->query($sql);
foreach($arr as $v){
echo"<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[3]}</td>
</tr>";
}
?>
</table>
</div>
</div>
</div>
</body>
购物车
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<link href="main.css" rel="stylesheet" type="text/css">
<style type="text/css">
#st{
background-color:#390;
color:#FFF;
font-size:20px;
margin-left:450px;
}
</style>
</head>
<?php
session_start();
if(empty($_SESSION["uid"])){
header("location:login.php");
exit;
}
?>
<body>
<form action="dingdantijiao.php" method="post">
<div>
<div id="shang"><h2>购物车</h2></div>
<div>
<div id="zuo">
<div class="caidan"><a href="main.php">浏览商品</a></div>
<div class="caidan"><a href="zhanghu.php">查看账户</a></div>
<div class="caidan"><a href="gouwuche.php">查看购物车</a></div>
</div>
<div id="you">
<div id="yous">购物车有以下商品</div>
<table border="1" bordercolor="#CCFFFF" width="100%">
<tr bgcolor="#339999" height="40">
<td>商品名称</td>
<td>商品单价</td>
<td>购买数量</td>
<td>操作</td>
</tr>
<?php
require_once "../DBDA.class.php";
$db = new DBDA();
$sql = "select * from ordertails";
$arr = $_SESSION["gwc"];
foreach($arr as $v){
$sql = "select name,price from fruit where ids='{$v[0]}'";
$name = $db->query($sql);
echo "<tr>
<td>{$name[0][0]}</td>
<td>{$name[0][1]}</td>
<td>{$v[1]}</td>
<td><a href='del.php?code={$v[0]}'><input type='button' value='删除' /></a></td>
</tr>";
}
?>
</table>
<br>
<a href="dingdantijiao.php?ids={$v[0]}" rel="external nofollow" id="st">提交订单</a>
</div>
</div>
</div>
</form>
</body>
</html>
删除商品处理
<?php
session_start();
$code = $_GET["code"];
$arr = $_SESSION["gwc"];
//var_dump($arr);
//取索引2(数量)
foreach ($arr as $k=>$v)
{
if($v[0]==$code)
{
if($v[1]>1){
//要删除的数据
$arr[$k][1]-=1;
}
else{
//数量为1的情况下,移除该数组
unset($arr[$k]);
}
}
}
$_SESSION["gwc"] = $arr;
//记得扔到session里面
header("location:gouwuche.php");
//删除完跳转回去
提交订单
<?php
session_start();
$ids = $_GET["ids"];
//查看余额
$uid = $_SESSION["uid"];
require_once "../DBDA.class.php";
$db = new DBDA();
$sql = "select account from login where username='{$uid}'";
$arr = $db->query($sql,0);
$aye = $arr[0][0];//余额
//var_dump($aye);
if(!empty($_SESSION["gwc"])){
$arr = $_SESSION["gwc"];
$sum = 0;
//$numbers = count($arr);
foreach($arr as $v){
$sql = "select * from fruit where ids='{$v[0]}'";
$price = $db->query($sql,0);
$dj = $price[0][2];
$sum = $sum+$dj*$v[1];
}
}else{
echo "您还未购买商品!";
//header("shopping_list.php");
exit;
}
//判断余额是否满足购买
if($aye>=$sum){
//判断库存
foreach($arr as $v){
$skc = "select name,numbers from fruit where ids='{$v[0]}'";
$akc = $db->query($sql,0);
//var_dump($akc);
$kc = $akc[0][4];//库存
//var_dump($kc);
if($kc<$v[1]){
echo "库存不足!";
exit;
}
}
//提交订单
//账户扣除余额
$skye = "update login set account=account-'{$sum}' where username='{$uid}'";
$zhye = $db->query($skye,1);
//扣除库存
foreach($arr as $v){
$skckc = "update fruit set numbers=numbers-'{$v[1]}' where ids='{$v[0]}'";
$sykc = $db->query($skckc,1);
}
//添加订单
$ddh = date("Y-m-d H:i:s");
$time = time();
$stjd = "insert into orders values('{$time}','{$uid}','{$ddh}')";
$wcdh = $db->query($stjd,1);
//添加订单详情
foreach($arr as $v){
$ddxq = "insert into orderdetails values('','{$ddh}','{$v[0]}','{$v[1]}')";
$axq = $db->query($ddxq,1);
}
}else{
echo "余额不足,请充值!";
exit;
}
unset($_SESSION["gwc"]);
header("location:main.php");