为实现简单的购物功能(购物车添加、账户查看、购物车商品删除、实时的购物商品数量及价格的计算显示、购物车商品数量可手动输入等),用session实现了一简单的以php语言为基础、连接MySQL数据库的购物车系统。(程彦瑞)

为了实现用户移动及pc端的适应,用媒体查询实现了响应式效果(文章最后实现)。

程序效果

登录界面:

添加购物车界面(购买数量和总价会在界面动态显示):

 可以通过在文本框里输入数字来控制购买数量, 点击删除删除整行,点击清空清所有数据。

不可在文本框里输入小于零或者其他违法字符:

提交订单界面:

点返回购物车中数据没有发生变化,点确认购买库存及账户余额都会相应减少:

点击购买时会先判断余额,如果不足会显示余额不足;然后判断库存,如果不足会显示库存不足;最条件都成立显示购买成功。

其中登录信息以及商品信息从数据库中导出:

需要用到的数据库信息:

订单选项会显示之前的购买记录:

 点击会显示详细信息:

 

做的比较快,大体框架比较简单,还有些许内容待补充、完善。

 

首先登陆界面代码比较简单,直接上代码:

<!doctype html  HTML>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>
	<style type="text/css">
* { margin: 0px auto; padding: 0px}
#backimg{
background-image: url('Login/bg.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 100%;
height: 100%;

}
#denglu{
background-image: url('Login/bg2.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 40%;
height: 48%;
position: absolute;
left: 30%;
top: 23%
}
#huanchong{
background-image: url('Login/login-v2-load_02.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 30%;
height: 12%;
cursor: pointer;
}
#huanchong1{
background-image: url('Login/login-v2-load_01.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 28%;
height: 8%;
cursor: pointer;
text-align: right;
padding-top: 4%;
padding-right: 2%;
}
#shuru{
left: 20%;
width: 70%;
position: relative;
top: 40%;
}
#uid{
width: 100%;
}
#pwd{
padding-top: 8%;
padding-bottom: 8%;
width: 100%;
}
	</style>
	<body style=" height: 100%">
		<div id="backimg">
			<div id="denglu" >
				<div id="shuru">
					<form action="dengluchuli.php" method="post" id="myform">
						<div id="uid">
							用户名:
							<input type="text" name="uid"  style="width: 40%; height: 8%" />
						</div>
						<div  id="pwd" >
							密    码:
							<input type="text" name="pwd" style="width: 40%; height: 8%"  />
						</div>
						<div id="huanchong" onclick="denglu()" style="display: block;"></div>
						<div id="huanchong1" style="display: none;">
							<img src="Login/loading.gif" alt="" />
						</div>
					</form>
				</div>
			</div>
		</div>

	</body>
	<script type="text/javascript">function denglu() {
	var a=document.getElementById("huanchong");
	var b=document.getElementById("huanchong1");
	a.style.display="none";
	b.style.display="block";
	document.getElementById("myform").submit();
}</script>
</html>

  

横向长度使用的百分比以,纵向也是用的百分比,前提是使用html5 或者html4 的头部声明:

<!doctype html  HTML>

body标签设置百分比为100%:

<body style=" height: 100%">

登录时失败会直接显示失败,由于做得比较简单,没有进行完善就直接输出了:

登陆界面的php跳转代码,首先我做了一个登录类(LOGIN.class.php),只是为了以后方便:

<?php
class DBDA {
	public $host = "localhost";
	public $uid = "root";
	public $pwd = "";
	public $dbname = "12345";

	//成员方法
	public function Query($sql, $type = 1) {
		$db = new MySQLi($this -> host, $this -> uid, $this -> pwd, $this -> dbname);
		$r = $db -> query($sql);

		if ($type == 1) {
			return $r -> fetch_all();
		} else {
			return $r;
		}
	}

}

class LoGin {
	public static $uid;
	public static $pwd;
	function __construct($x, $y) {
		self::$uid = $x;
		self::$pwd = $y;
	}

	static function logi($table, $username, $password, $name) {
		$db = new DBDA();
		$nnn = self::$uid;
		$sql = " select $name,$password from $table where $username='$nnn '";
		$at = $db -> Query($sql);
		$p = self::$pwd;
		if (!empty($p) && $p == $at[0][1]) {
			$_SESSION["uid"] = $at[0][0];
			header("location:main.php");
		} else {
			echo "用户名或密码错误";
		}
	}

}
?>

  

还有一个访问数据库的类(DBDA.class.php),直接上代码:

<?php
class DBDA {
	public $host = "localhost";
	public $uid = "root";
	public $pwd = "";
	public $dbname = "12345";

	//成员方法
	public function Query($sql, $type = 1) {
		$db = new MySQLi($this -> host, $this -> uid, $this -> pwd, $this -> dbname);
		$r = $db -> query($sql);

		if ($type == 1) {
			return $r -> fetch_all();
		} else {
			return $r;
		}
	}

}

  

使用时直接输入变量和登录数据库的信息就可以了(dengluchuli.php):

<?php
session_start();
include("LOGIN.class.php");
$dd = new LoGin($_POST["uid"],$_POST["pwd"]);
$ae = $dd::logi("login","username","password","name");
?>

 

登录界面做完之后是主界面:

<!doctype html HTML>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>
	<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#right {
		position: relative;
		float: right;
		width: 60%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
	table {
		text-align: center;
	}
	table tr td {
		height: 40px;
	}
	table tr th {
		height: 40px;
	}
</style>
	<body style="height: 100%;">

		<div id="back">
			<div id="top" >
				大苹果购物网

			</div>
			<div id="denglu">
				欢迎您,
				<span style=" color:white;"> <?php
				session_start();

				if (empty($_SESSION["uid"])) {
					header("location:denglu.php");
					exit ;
				}
				include ("DBDA.class.php");

				echo $_SESSION["uid"];
				?> </span>
				!
			</div>

			<div style="height: 70%; ">
				<div id="left">
					<ul>
						<li>
							<a href="main.php">
								浏览商品
							</a>
						</li>
						<li>
							<a href=" zhanghu.php ">
								查看账户
							</a>
						</li>
						<li>
							<a href="gouwuche.php">
								查看购物车
							</a>
						</li>
						<li>
							<a href="dingdan.php">
								订单信息
							</a>
						</li>
					</ul>
				</div>
				<div id="right">

					<?php

					if (!empty($_SESSION["gwc"])) {
						$atter = $_SESSION["gwc"];
						$sum = 0;
						$total = 0;
						foreach ($atter as $v) {
							$db = new DBDA();
							$sql = " select price from fruit where name = '{$v[0]}' ";
							$at1 = $db -> Query($sql);
							$total = $total + $at1[0][0] * $v[1];
							$sum = $sum + $v[1];
						}
						echo "<div style=' background-color: white;  width: 100%; ' >购物车中有{$sum}件商品,总价格为:{$total} 元。</div>";

					}
				?>

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr style="background-color: darksalmon;"><th>代号</th><th>水果名称</th><th>水果价格</th><th>原产地</th><th>货架</th><th>库存量</th><th></th></tr>
<?php
$dd = new DBDA();
$sql1 = " select * from fruit ";
$at = $dd -> Query($sql1);
for ($i = 0; $i < count($at); $i++) {
	if ($i % 2 == 0) {
		echo " <tr style=' background-color:  cornflowerblue ;'>
<td>  {$at[$i][0]}</td>
<td>{$at[$i][1]}</td>
<td>{$at[$i][2]}</td>
<td>{$at[$i][3]}</td>
<td>{$at[$i][4]}</td>
<td>{$at[$i][5]}</td>
<td><a href='jia.php?name={$at[$i][1]}' onclick=\"return confirm('确定购买么')\">购买 </a></td>
</tr> ";
	} else {
		echo " <tr style='background-color:whitesmoke ;'>
<td>  {$at[$i][0]}</td>
<td>{$at[$i][1]}</td>
<td>{$at[$i][2]}</td>
<td>{$at[$i][3]}</td>
<td>{$at[$i][4]}</td>
<td>{$at[$i][5]}</td>
<td><a href='jia.php?name={$at[$i][1]}' onclick=\"return confirm('确定购买么')\">购买 </a></td>
</tr> ";
	}

}
					?>

					</table>
				</div>
			</div>
			<div id="bottom" style="padding-top: 1%;">
				发瑞
			</div>
		</div>
	</body>

</html>

  

然后是实现点击购买动态的在表格上方显示商品数量和总价(jia.php):

<?php
session_start();
if (empty($_SESSION["uid"])) {
	header("location:denglu.php");
	exit ;
}
$name = $_GET["name"];
include ("DBDA.class.php");
$db = new DBDA();
$sql = " select price,numbers from fruit where name='{$name}' ";
$at = $db -> Query($sql);
if (empty($_SESSION["gwc"])) {
	$atter = array( array($name, 1, $at[0][0], $at[0][1]));
	$_SESSION["gwc"] = $atter;
} else {
	$atter = $_SESSION["gwc"];
	if (panduan($name, $atter)) {
		for ($i = 0; $i < count($atter); $i++) {
			if ($name == $atter[$i][0]) {
				$atter[$i][1]++;
			}
		}
		$_SESSION["gwc"] = $atter;
	} else {
		$arr = array($name, 1, $at[0][0], $at[0][1]);
		$atter[] = $arr;
		$_SESSION["gwc"] = $atter;
	}
}
function panduan($name, $atter) {
	$n = 0;
	foreach ($atter as $v) {
		if ($name == $v[0]) {
			$n++;
		}
	}
	if ($n == 0) {
		return false;
	} else {
		return true;
	}
}

header("location:main.php");
?>

  

查看账户界面比较简单,就是直接访问数据库就可以了:

<!doctype html HTML>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>
	<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#he {
		height: 70%;
	}
	#right {
		background-color: white;
		position: relative;
		float: right;
		width: 55%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
		margin-right: 5%;
		margin-top: 2%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
</style>
	<body style="height: 100%;">

		<div id="back">
			<div id="top" >
				大苹果购物网

			</div>
			<div id="denglu">
				欢迎您,
				<span style=" color:white;"> <?php
				session_start();

				if (empty($_SESSION["uid"])) {
					header("location:denglu.php");
					exit ;
				}
				include ("DBDA.class.php");

				echo $_SESSION["uid"];
				?></span>
				!
			</div>

			<div  id="he">
				<div id="left">
					<ul>
						<li>
							<a href="main.php">
								浏览商品
							</a>
						</li>
						<li>
							<a href="zhanghu.php">
								查看账户
							</a>
						</li>
						<li>
							<a href="gouwuche.php">
								查看购物车
							</a>
						</li>
						<li>
							<a href="dingdan.php">
								订单信息
							</a>
						</li>
					</ul>
				</div>
				<div id="bao">

					<div id="right">
						您的账户中还有余额
						<span style="color: red;" > <?php

						$uid = $_SESSION["uid"];

						$db = new DBDA();
						$sql = " select account from login where name='{$uid}' ";
						$at = $db -> Query($sql);
						echo $at[0][0];
						?></span>

						元。
					</div>
				</div>
			</div>
			<div id="bottom" style="padding-top: 1%;">
				发瑞
			</div>
		</div>
	</body>

</html>

  

 

 查看订单界面(dingdan.php)也比较简单,但是数据库中的内容是在提交订单界面,把数据传入数据库的,然后这个界面直接访问数据库就可以了:

<!doctype html HTML>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>

<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#right {
		position: relative;
		float: right;
		width: 60%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
	table {
		text-align: center;
	}
	table tr td {
		height: 40px;
	}
	table tr th {
		height: 40px;
	}
</style>
<body style="height: 100%;">

<div id="back">
<div id="top" >
大苹果购物网

</div>
<div id="denglu">
欢迎您,
<span style=" color:white;">
<?php
session_start();

if (empty($_SESSION["uid"])) {
	header("location:denglu.php");
	exit ;

}

echo $_SESSION["uid"];
?>
</span>
!
</div>

<div style="height: 70%; ">
<div id="left">
<ul>
<li><a href="main.php">浏览商品</a></li>
<li><a href="zhanghu.php">查看账户</a></li>
<li><a href="gouwuche.php">查看购物车</a></li>
<li><a href="dingdan.php">订单信息</a></li>
</ul>
</div>
<div id="right">
<table border="0" cellspacing="0" cellpadding="0" width="90%">
<tr style="background-color:salmon;"><th>订单号</th><th>订购人</th><th>订购时间</th><th></th></tr>

<?php

include ("DBDA.class.php");
$db = new DBDA();
$sql = "select * from orders ";
$v = $db -> Query($sql);
for ($i = 0; $i < count($v); $i++) {
	if ($i % 2 == 0) {
		echo " <tr style='background-color: burlywood;'><td> {$v[$i][0]}</td><td> {$v[$i][1]}</td><td> {$v[$i][2]}</td><td><a href='chakan.php?code={$v[$i][0]}'> 查看详情</a></td></tr> ";
	} else {
		echo "<tr style='background-color: darkgrey;'><td> {$v[$i][0]}</td><td> {$v[$i][1]}</td><td> {$v[$i][2]}</td><td><a href='chakan.php?code={$v[$i][0]}'> 查看详情</a></td></tr>";
	}
}
?>
</table>

</div>
</div>
<div id="bottom" style="padding-top: 1%;">
发瑞
</div>
</div>
</body>
</html>

  

查看详情(chakan.php)也是直接通过上一层的a标签get一个索引:

<!doctype html HTML>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>

<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#right {
		position: relative;
		float: right;
		width: 60%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
	table {
		text-align: center;
	}
	table tr td {
		height: 40px;
	}
	table tr th {
		height: 40px;
	}
</style>
<body style="height: 100%;">

<div id="back">
<div id="top" >
大苹果购物网

</div>
<div id="denglu">
欢迎您,
<span style=" color:white;">
<?php
session_start();

if (empty($_SESSION["uid"])) {
	header("location:denglu.php");
	exit ;
}

echo $_SESSION["uid"];
?>
</span>
!
</div>

<div style="height: 70%; ">
<div id="left">
<ul>
<li><a href="main.php">浏览商品</a></li>
<li><a href="zhanghu.php">查看账户</a></li>
<li><a href="gouwuche.php">查看购物车</a></li>
<li><a href="dingdan.php">订单信息</a></li>
</ul>
</div>
<div id="right">
<table border="0" cellspacing="0" cellpadding="0" width="90%">
<tr style="background-color:salmon;"><th>水果名称</th><th></th><th>水果数量</th></tr>

<?php

include ("DBDA.class.php");
$db = new DBDA();

$sql = "select fruitname,count from orderdetails where ordercode='{$_GET["code"]}' ";
$v = $db -> Query($sql);

for ($i = 0; $i < count($v); $i++) {
	if ($i % 2 == 0) {
		echo " <tr style='background-color: burlywood;'><td> {$v[$i][0]}</td><td> ×</td><td> {$v[$i][1]}</td></tr>";
	} else {
		echo "<tr style='background-color: darkgrey;'><td> {$v[$i][0]}</td><td> ×</td><td> {$v[$i][1]}</td></tr>";
	}
}
?>
</table>

</div>
</div>
<div id="bottom" style="padding-top: 1%;">
发瑞
</div>
</div>
</body>
</html>

  

 

 这个项目主要是查看购物车界面比较复杂,需要多条件判断,引入多个session。

 首先购物车里的信息是放在   $_SESSION["gwc"]     里面,

 还有 购物商品总数  $_SESSION["sum"]   购物商品总价格  $_SESSION["total"] 。订单确认之后访问数据库减少相应金额,订单数据同时也导入数据库,同时库存也相应减少。

 

上代码(gouwuche.php):

<!doctype html HTML>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>

	<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#right {
		position: relative;
		float: right;
		width: 60%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
	table {
		text-align: center;
	}
	table tr td {
		height: 40px;
	}
	table tr th {
		height: 40px;
	}
</style>
	<body style="height: 100%;">

		<div id="back">
			<div id="top" >
				大苹果购物网

			</div>
			<div id="denglu">
				欢迎您,
				<span style=" color:white;"> <?php
				session_start();

				if (empty($_SESSION["uid"])) {
					header("location:denglu.php");
					exit ;
				}
				if (!empty($_SESSION["sz"])) {
					echo " <script>alert('输入须为大于零的数')   </script> ";
					$_SESSION["sz"] = '';
				}

				echo $_SESSION["uid"];
				?> </span>
				!
			</div>

			<div style="height: 70%; ">
				<div id="left">
					<ul>
						<li>
							<a href="main.php">
								浏览商品
							</a>
						</li>
						<li>
							<a href="zhanghu.php">
								查看账户
							</a>
						</li>
						<li>
							<a href="gouwuche.php">
								查看购物车
							</a>
						</li>
						<li>
							<a href="dingdan.php">
								订单信息
							</a>
						</li>
					</ul>
				</div>
				<div id="right">

					<table border="0" cellspacing="0" cellpadding="0" width="80%">
						<tr style="background-color: darksalmon;">
							<th colspan="5">购物车中有以下商品</th>
						</tr>
						<tr style="background-color:whitesmoke ; ">
							<td>商品名称</td><td>商品单价</td><td>购买数量</td><td>库存</td><td></td>
						</tr>

						<form action="tijiao.php" method="post">

						<?php

						if (!empty($_SESSION["gwc"])) {
							$_SESSION["gwc"] = array_values($_SESSION["gwc"]);
							$atter = $_SESSION["gwc"];
							for ($i = 0; $i < count($atter); $i++) {
								if ($i % 2 == 0) {
									echo " <tr style='background-color:cornflowerblue;'><td>{$atter[$i][0]} </td><td>{$atter[$i][2]} </td> <td><input type='text' name='{$atter[$i][0]}'  value='{$atter[$i][1]}'  style='width:20%' />  </td><td>{$atter[$i][3]}</td> <td> <a href ='shanchu.php?name={$i}' onclick=\"return confirm('确定删除么')\">删除</a> </td></tr> ";
								} else {
									echo " <tr style='background-color:whitesmoke ;'><td>{$atter[$i][0]} </td><td>{$atter[$i][2]} </td><td><input type='text' name='{$atter[$i][0]}'  value='{$atter[$i][1]}'  style='width:20%' />  </td><td>{$atter[$i][3]}</td><td> <a href ='shanchu.php?name={$i}' onclick=\"return confirm('确定删除么')\">删除</a> </td></tr> ";
								}
							}
						}
					?>
<tr style='text-align: right;'><td colspan='5'> <input type='submit'    value='提交订单' style='width: 20%; height: 100%;'  />
</form>
<div id="xianshi" style=" float: left; ">
<?php

if (!empty($_SESSION["low"])) {
	echo $_SESSION["low"];
	unset($_SESSION["low"]);
} else if (!empty($_SESSION["cun"])) {
	echo $_SESSION["cun"];
	unset($_SESSION["cun"]);
} else {
	if (!empty($_SESSION["success"])) {
		echo $_SESSION["success"];
		unset($_SESSION["success"]);
	}

}
						?>
						</div>

						</td> </tr>
						<tr style='text-align: right;'>
							<td colspan='5'>
								<a href="qingkong.php">
									<input type="button"  style='width: 20%; height: 100%;' value="清空购物车" />
								</a></td>
						</tr>
					</table>

				</div>
			</div>
			<div id="bottom" style="padding-top: 1%;">
				发瑞
			</div>
		</div>
	</body>
</html>

  

没有添加购物车是是空。

 

添加过商品了:

 

通关文本框输入数字来操作购买数量:

存货不足时:

余额不足时:

判断条件会先判断余额,在判断库存,最后点击提交订单确认确认:

确认之前会跳转一个处理界面(tijiao.php)是判断输入信息是否合法,输入库存是否符合,余额是否符合:

<?php
session_start();
if (empty($_SESSION["uid"])) {
	header("location:denglu.php");
	exit ;
}
$atter = $_SESSION["gwc"];
for ($i = 0; $i < count($atter); $i++) {
	if (!empty($_POST[$atter[$i][0]])) {
		$sz = $_POST[$atter[$i][0]];
		if (is_numeric($sz) && $sz > 0) {
			$atter[$i][1] = $_POST[$atter[$i][0]];
		} else {
			$_SESSION["sz"] = "输入须为大于零的数字";
			header("location:gouwuche.php");
			exit ;
		}

	}
}
$_SESSION["gwc"] = $atter;

include ("DBDA.class.php");
//求商品数和总价格
$sum = 0;
$total = 0;
for ($i = 0; $i < count($atter); $i++) {
	$dbd = new DBDA();
	$sqld = " select price from fruit where name = '{$atter[$i][0]}' ";
	$at1 = $dbd -> Query($sqld);
	$total = $total + $at1[0][0] * $atter[$i][1];
	$sum = $sum + $atter[$i][1];
}

$uid = $_SESSION["uid"];

$db = new DBDA();
$sql = " select account from login where name = '{$uid}' ";
$ss = $db -> Query($sql);

if (!empty($atter) && $ss[0][0] < $total) {
	$_SESSION["low"] = "账户余额不足";
	header("location:gouwuche.php");
} else {
	$cunbuzu = "";
	foreach ($atter as $v) {
		if ($v[1] > $v[3]) {
			$cunbuzu = $cunbuzu . $v[0] . "的存货不足。";
			$_SESSION["cun"] = $cunbuzu;

		}

	}
	if (!empty($cunbuzu)) {
		header("location:gouwuche.php");
		exit ;
	}
	if (empty($cunbuzu) && !empty($atter)) {
		$_SESSION["price"] = $total;
		$_SESSION["sum"] = $sum;
		header("location:querengoumai.php");
	} else {
		header("location:gouwuche.php");
	}

}
?>

  

购物车中点击删除会删除本条数据,点击清空会删除所有数据(qingkong.php)。

<?php
session_start();
if(empty($_SESSION["uid"]))
{
header("location:denglu.php");
exit;
}
$_SESSION["gwc"] = "";
header("location:gouwuche.php");
?>

  

删除一条数据代码(shanchu.php):

<?php
session_start();
if(empty($_SESSION["uid"]))
{
header("location:denglu.php");
exit;
}
$atter = $_SESSION["gwc"];
$i = $_GET["name"];

if( count($atter)==1  )
{
	$atter = "";
}
else{
		unset($atter[$i]);
	}
$_SESSION["gwc"]=$atter;
header("location:gouwuche.php");
?>

  

确认界面点击返回购物车信息不变,账户余额不变,点击购买操作数据库和引入session。

直接上代码(querengoumai.php):

<!doctype html HTML>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Document</title>
	</head>
	<style type="text/css">
	* {
		margin: 0px auto;
		padding: 0px;
	}
	a {
		text-decoration: none;
	}
	#back {
		background-image: url(Login/bg.jpg);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		height: 100%;
		width: 100%;
	}
	#top {
		height: 10%;
		font-size: 36px;
		padding-top: 2%;
		padding-left: 4%;
		font-family: "微软雅黑";
		font-weight: 700;
		color: white;
	}
	#denglu {
		text-align: right;
		padding-right: 5%;
		font-size: 24px;
	}
	#left {
		padding-left: 15%;
		position: relative;
		float: left;
		height: 70%;
		padding-top: 10%;
	}
	#left ul li {
		display: block;
		margin-top: 20%;
		font-size: 24px;
	}
	#he {
		height: 70%;
	}
	#right {
		position: relative;
		float: right;
		width: 55%;
		padding-right: 5%;
		padding-top: 2%;
		height: 82%;
		margin-right: 5%;
		margin-top: 2%;
	}
	#bottom {
		font-size: 24px;
		color: white;
		text-align: center;
	}
</style>
	<body style="height: 100%;">

		<div id="back">
			<div id="top" >
				大苹果购物网

			</div>
			<div id="denglu">
				欢迎您,
				<span style=" color:white;"> <?php
				session_start();

				if (empty($_SESSION["uid"])) {
					header("location:denglu.php");
					exit ;
				}
				include ("DBDA.class.php");

				echo $_SESSION["uid"];
				?> </span>
				!
			</div>

			<div  id="he">
				<div id="left">
					<ul>
						<li>
							<a href="main.php">
								浏览商品
							</a>
						</li>
						<li>
							<a href="zhanghu.php">
								查看账户
							</a>
						</li>
						<li>
							<a href="gouwuche.php">
								查看购物车
							</a>
						</li>
						<li>
							<a href="dingdan.php">
								订单信息
							</a>
						</li>
					</ul>
				</div>
				<div id="bao">

					<div id="right">

						<table border="0" cellspacing="0" cellpadding="0" width="50%" style="text-align: center;">
							<tr style="background-color:red;">
								<th colspan="3">订单详情</th>
							</tr>
							<?php
							$atter = $_SESSION["gwc"];
							for ($i = 0; $i < count($atter); $i++) {
								if ($i % 2 == 0) {
									echo "<tr style='background-color: blueviolet;'><td> {$atter[$i][0]} </td><td>× </td><td> {$atter[$i][1]} </td></tr> ";
								} else {
									echo "<tr style='background-color:greenyellow;'><td> {$atter[$i][0]} </td><td>× </td><td> {$atter[$i][1]} </td></tr> ";
								}
								$total = $_SESSION["price"];
								$sum = $_SESSION["sum"];
							}
						?>
<tr style="background-color:white;"><td colspan="3">商品总价为<?php echo "$total"   ?>
							元</td></tr>
							<tr style="background-color:white;">
								<td colspan="3">
									<a href="goumaichuli.php">
										<input type="button" name="" id="" value="确认购买" />
									</a>
									<a href="gouwuche.php">
										<input type="button" name="" id="" value="返回购物车" />
									</a></td>
							</tr>
						</table>
					</div>
				</div>
			</div>
			<div id="bottom" style="padding-top: 1%;">
				发瑞
			</div>
		</div>
	</body>

</html>

  

 之后是购买提交之后的后台数据库处理界面(goumaichuli.php):

<?php
session_start();
if(empty($_SESSION["uid"]))
{
header("location:denglu.php");
exit;
}
include("DBDA.class.php");
$total = $_SESSION["price"];
$atter = $_SESSION["gwc"];
$uid = $_SESSION["uid"];
$db= new DBDA();
$sql = " select account from login where name = '{$uid}' ";
$ss = $db->Query($sql);
$ss[0][0] = $ss[0][0]-$total;
	    $_SESSION["success"] = "购买成功";
		$sql1 = " update login set account={$ss[0][0]} where name='{$uid }'  ";
		$zhifu = $db->Query($sql1,0);
		for($i =0 ;$i<count($atter);$i++)
		{
			$df = new DBDA();
			$sqq =  " select numbers from fruit where name = '{$atter[$i][0]}'  ";
			$tt = $df->Query($sqq);
			$tt[0][0] = $tt[0][0] -$atter[$i][1];
			$df1 = new DBDA();
			$sqq1 = " update fruit set numbers='{$tt[0][0]}' where name = '{$atter[$i][0]}' ";
			$tt1 = $df1->Query($sqq1,0);
		}

//订单表制作
$ddbzz = $_SESSION["gwc"];

$time = date("Y-m-d,h:i:s");
$sqldd = " select count(*) from orders ";
$nb = $df1->Query($sqldd);
$code = $nb[0][0];
$code = $num=str_pad($code,4,"0",STR_PAD_LEFT);
$sqlgmr = " select username from login where name = '{$uid}' ";
$nn = $df1->Query($sqlgmr);
$ordercode = $nn[0][0].$code;
$sqltj = " insert into orders values('{$ordercode}','{$uid}','{$time}') ";
$ntj= $df1->Query($sqltj,0);

//订单详情表

foreach($ddbzz as $n)
{
	$sqllz= " insert into orderdetails values('','{$ordercode}','{$n[0]}','{$n[1]}') ";
	$ordertj = $df1->Query($sqllz,0);
}

unset($_SESSION["gwc"] );
header("location:gouwuche.php")
?>

  

查看数据库,数据库中内容改变成功。

 

 

 代码还剩媒体查询内容,以登录界面做例子就是屏宽在范围之内用另一套css样式,直接代码:

@media only screen and (max-width: 500px){                 //屏宽小于500px
* { margin: 0px auto; padding: 0px}
#backimg{
background-image: url('Login/bg.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 100%;
height: 100%;

}
#denglu{
background-image: url('Login/bg2.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 80%;
height: 40%;
position: absolute;
left: 5%;
top: 23%
}
#huanchong{
background-image: url('Login/login-v2-load_02.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 30%;
height: 12%;
cursor: pointer;
}
#huanchong1{
background-image: url('Login/login-v2-load_01.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 28%;
height: 8%;
cursor: pointer;
text-align: right;
padding-top: 4%;
padding-right: 2%;
}
#shuru{
left: 20%;
width: 70%;
position: relative;
top: 40%;
}
#uid{
width: 100%;
}
#pwd{
padding-top: 8%;
padding-bottom: 8%;
width: 100%;
}
}
@media only screen and (min-width: 500px){                                   //屏宽大于500
* { margin: 0px auto; padding: 0px}
#backimg{
background-image: url('Login/bg.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 100%;
height: 100%;

}
#denglu{
background-image: url('Login/bg2.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 55%;
height: 38%;
position: absolute;
left: 20%;
top: 23%
}
#huanchong{
background-image: url('Login/login-v2-load_02.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 30%;
height: 10%;
cursor: pointer;
}
#huanchong1{
background-image: url('Login/login-v2-load_01.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 28%;
height: 7%;
cursor: pointer;
text-align: right;
padding-top: 4%;
padding-right: 2%;
}
#shuru{
left: 20%;
width: 70%;
position: relative;
top: 40%;
}
#uid{
width: 100%;
}
#pwd{
padding-top: 8%;
padding-bottom: 8%;
width: 100%;
}
}
@media only screen and (min-width: 768px){                                  //屏宽768以上
* { margin: 0px auto; padding: 0px}
#backimg{
background-image: url('Login/bg.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 100%;
height: 100%;

}
#denglu{
background-image: url('Login/bg2.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 50%;
height: 48%;
position: absolute;
left: 20%;
top: 23%
}
#huanchong{
background-image: url('Login/login-v2-load_02.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 30%;
height: 12%;
cursor: pointer;
}
#huanchong1{
background-image: url('Login/login-v2-load_01.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 28%;
height: 8%;
cursor: pointer;
text-align: right;
padding-top: 4%;
padding-right: 2%;
}
#shuru{
left: 20%;
width: 70%;
position: relative;
top: 40%;
}
#uid{
width: 100%;
}
#pwd{
padding-top: 8%;
padding-bottom: 8%;
width: 100%;
}
}
@media only screen and (min-width: 1024px){                               //屏宽1024以上
* { margin: 0px auto; padding: 0px}
#backimg{
background-image: url('Login/bg.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 100%;
height: 100%;

}
#denglu{
background-image: url('Login/bg2.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 40%;
height: 48%;
position: absolute;
left: 30%;
top: 23%
}
#huanchong{
background-image: url('Login/login-v2-load_02.jpg');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 30%;
height: 12%;
cursor: pointer;
}
#huanchong1{
background-image: url('Login/login-v2-load_01.png');
background-size: 100% 100%;
background-repeat:no-repeat;
width: 28%;
height: 8%;
cursor: pointer;
text-align: right;
padding-top: 4%;
padding-right: 2%;
}
#shuru{
left: 20%;
width: 70%;
position: relative;
top: 40%;
}
#uid{
width: 100%;
}
#pwd{
padding-top: 8%;
padding-bottom: 8%;
width: 100%;
}
}
	</style>

  

会实现:

响应式

其他是相同道理:

session实现购物车的更多相关文章

  1. 会话技术之Session(购物车加入、查看和清空)

    会话技术之Session session:服务器端会话技术 当我们第一次访问的服务器的时候,服务器获取id, 能获取id 要拿着这个id去服务器中查找有无此session 若查找到了:直接拿过来将数据 ...

  2. 【ASP】session实现购物车

    1.问题提出 利用session内置对象,设计并实现一个简易的购物车,要求如下: 1)利用用户名和密码,登录进入购物车首页 2)购物首页显示登录的用户名以及该用户是第几位访客.(同一用户的刷新应该记录 ...

  3. Session 简单购物车

    package session.test; import java.io.IOException; import java.io.PrintWriter; import java.util.Linke ...

  4. 11月14日用AJAX、PHP、SESSION做购物车

    购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  5. ajax、PHP、session做购物车

    购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  6. 用ajax、PHP、session做购物车

    购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  7. Session版购物车+MVC局部刷新

     效果图: 大致代码: <script type="text/javascript"> $(function () { LoadOrderDetailList(); } ...

  8. 使用session页面控制登录入口及购物车效果的实现

          由于 Session 是以文本文件形式存储在服务器端的,所以不怕客户端修改 Session 内容.实际上在服务器端的 Session 文件,PHP 自动修改 Session 文件的权限,只 ...

  9. [原创]java WEB学习笔记33:Session 案例 之 购物车

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

随机推荐

  1. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑

    阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...

  2. LDR详解

    ARM指令集中,LDR通常都是作加载指令的,但是它也可以作伪指令. LDR伪指令的形式是"LDR Rn,=expr".下面举一个例子来说明它的用法. COUNT EQU       ...

  3. C语言 · 4-3水仙花数

    问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 153=1^3+5^3+3^3. 样例输入 一个满足题目要求的输入范例.例:无 样例输出 ...

  4. docker for mac 学习记录

    docker基本命令 docker run -d -p 80:80 --name webserver nginx 运行容器并起别名 docker ps 展示目前启动的容器 docker ps -a 展 ...

  5. UWP开发之Mvvmlight实践七:如何查找设备(Mobile模拟器、实体手机、PC)中应用的Log等文件

    在开发中或者后期测试乃至最后交付使用的时候,如果应用出问题了我们一般的做法就是查看Log文件.上章也提到了查看Log文件,这章重点讲解下如何查看Log文件?如何找到我们需要的Packages安装包目录 ...

  6. B样条基函数的定义和性质

    定义:令U={u0,u1,…,um}是一个单调不减的实数序列,即ui≤ui+1,i=0,1,…,m-1.其中,ui称为节点,U称为节点矢量,用Ni,p(u)表示第i个p次(p+1阶)B样条基函数,其定 ...

  7. 用神奇的currentColor制作简洁的颜色动画效果

    先上一个兼容性总结图:老版本ie可以直接用复杂方法了,套用某表情包的话:  2016年了,做前端你还考虑兼容IE6?你这简直是自暴自弃! 好了,知道了兼容性,我们可以放心的使用了. 在CSS3中扩展了 ...

  8. 基于RN开发的一款视频配音APP(开源)

    在如今React.ng.vue三分天下的格局下,不得不让自己加快学习的脚步.虽然经常会陷入各种迷茫,学得越多会发现不会的东西也被无限放大,不过能用新的技术作出一些小项目小Demo还是会给自己些许自信与 ...

  9. iptables

    一.在服务器上打开 22.80.9011端口: iptables -A INPUT -p tcp --dport 9011 -j ACCEPT iptables -A OUTPUT -p tcp -- ...

  10. 2Sum

    用哈希表(unordered_map)使得时间复杂度从O(n*n)降到O(n),空间复杂度从O(1)增到O(n):一边找一边插入哈希表 注意 在C++11以前要使用unordered_map需要 #i ...