为实现简单的购物功能(购物车添加、账户查看、购物车商品删除、实时的购物商品数量及价格的计算显示、购物车商品数量可手动输入等),用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. Java多线程基础——对象及变量并发访问

    在开发多线程程序时,如果每个多线程处理的事情都不一样,每个线程都互不相关,这样开发的过程就非常轻松.但是很多时候,多线程程序是需要同时访问同一个对象,或者变量的.这样,一个对象同时被多个线程访问,会出 ...

  2. 如何选择PHP框架?

    PHP是世界上最受欢迎的编程语言之—.最近发布的PHP7令这种服务器的编程语言比以前变得更好,更稳定了. PHP被广泛应用于重大的项目.例如Facebook就是使用PHP来维护和创建它们的内部系统的. ...

  3. 如何快速优化手游性能问题?从UGUI优化说起

    WeTest 导读   本文作者从自身多年的Unity项目UI开发及优化的经验出发,从UGUI,CPU,GPU以及unity特有资源等几个维度,介绍了unity手游性能优化的一些方法.   在之前的文 ...

  4. Python自然语言处理工具小结

    Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...

  5. 解决IE8下不兼容rgba()的解决办法

    rgba()是css3的新属性,所以IE8及以下浏览器不兼容,这怎么办呢?终于我找到了解决办法. 解决办法 我们先来解释以下rgba rgba: rgba的含义,r代表red,g代表green,b代表 ...

  6. 计算Div标签内Checkbox个数或已被disabled的个数

    先看下面的html: 计算div内的checkbox个数:$('#divmod input[type="checkbox"]').length 计算div内checkbox被dis ...

  7. 端盘子的服务生到月薪一万五的IT精英,你能相信吗

    一直以来,我都觉得自己不是一个有故事的人. 以前的我,是个乖宝宝,对父母言听计从,特别内向,甚至一度感觉到自卑.不上学之后,我干过送货员,去工地除泥搬砖,当过油漆工,去过工厂,还去饭店当过端盘子的服务 ...

  8. mono for android学习过程系列教程(1)

    直接进入主题,关于mono for android的学习,首先配置好环境,如何配置环境,度娘谷歌一大堆,记得使用破解版. 我自己是百度“黑马四期”传智播客的视频,里面有破解版开发环境的软件. 今天直接 ...

  9. Lesson 15 Good news

    Text The secretary told me that Mr. Harmsworth would see me. I felt very nervous when I went into hi ...

  10. 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock

    ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...