PHP+MYSQL购物车:通俗易懂理解等你来哦+我的博客里面有核心解释

create table if not exists shop_goods(
	id int unsigned auto_increment key,
	gname varchar(50) not null unique,
	price decimal(10,2) not null default 0,
	number int unsigned not null default 0,
	pic varchar(50) NOT NULL DEFAULT 'jd.jpg'
);

//购物需要的数据篇

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		
	</style>
</head>
<body>
	<?php
		for ($i=1; $i < 50; $i++) { 
			# code...
			$pic=mt_rand(1,4);
			$sql="insert shop_goods(gname,price,pic) values('apple{$i}','488{$i}','{$pic}.jpg');";
			echo $sql."<br />";
		}
	?>
</body>
</html>

//下面的页面都是重要页面

<?php
$link=mysqli_connect('localhost','root','','test44');
mysqli_set_charset($link,'utf8');
$sql="select id,gname,price,pic from shop_goods";
//然后是运行
$result=mysqli_query($link,$sql);
//存在并且有条数就执行下面的啊
if($result&&mysqli_num_rows($result)>0)
{//这里是显示$result里面的数据,并赋给$row并且赋值到一个新数组里面啊,因为不知道多少条数据所以为[]
	while($row=mysqli_fetch_assoc($result))
	{
		$rows[]=$row;
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">

		*{padding: 0px;margin: 0px;}
		#container{width: 960px;border: 1px solid red;}
		.pro{width: 200px;border: 1px solid red;float: left;
		margin-left: 30px;margin-bottom: 10px;text-align: center;
		}
		.clearfix
		{
			overflow: hidden;zoom:1;clear: both;
		}
	</style>
</head>
<body>
	<a href="gw3.php">查看购物车</a>
	<div id="container" class='clearfix'>
		<?php foreach($rows as $row):?>
		<div  class="pro">
			<p><img src="./<?php
				echo $row['pic'];
			?>"></p>
			<p>名称:<?php
				echo $row['gname'];
			?></p>
			<p>价格:<?php
				echo $row['price']
			?></p>
			<p><a href="gw2.php?act=addCart&id=<?php
				echo $row['id'];
			?>">加入购物车</a></p>
		</div>
		<?php
			endforeach;
		?>
	</div>
</body>
</html>
<?php
error_reporting('E_ALL&~E_NOTICE');

header('content-type:text/html;charset=utf-8');
session_start();
$act=$_GET['act'];
//然后是获取id的值
$id=(int)$_GET['id'];
//然后是连接数据库
$link=mysqli_connect('localhost','root','','test44');
//然后是字符集
mysqli_set_charset($link,'utf8');
//然后是添加购物车操作
//当购物车有物品并且点击购物车的时候就执行
switch($act)
{
	case 'addCart':
	//添加购物车的id,看看是哪一条物品(编号)
	$sql="select * from shop_goods where id='{$id}'";
	//然后是运行把
	$result=mysqli_query($link,$sql);//运行起来如果有数据并且数据是一条的话就执行下面的啊
	//然后是存在也就是有东西并且是一条的话就
	if($result&&mysqli_affected_rows($link)==1)//意思是找到了
	{
		//就取出来
		$row=mysqli_fetch_assoc($result);
		//然后是检测商品是否存在
		if(!isset($_SESSION['cart']))
		{
			$_SESSION['cart']=array();//为空
		}
		//如果存在的话就+1
		if(array_key_exists($row['id'],$_SESSION['cart']))//判断键名是否存在与数组中啊,第二个是哪一个数组中啊
		{
			$_SESSION['cart'][$row['id']]['count']++;
		}else
		{
			$row['count']=1;//放count给数组中,并且为1
			$_SESSION['cart'][$row['id']]=$row;
		}
		exit("<script>alert('加入购物车成功');
				location.href='gw1.php';
			</script>");
	}
	break;
	case  'delGood':
	unset($_SESSION['cart'][$id]);
	exit("<script>alert('删除成功');
				location.href='gw3.php';
			</script>");
	break;
	case 'delCart':
	if(isset($_COOKIE[session_name()]))
	{
		setcookie(session_name(),session_id(),time()-1);
	}
	$_SESSION=array();
	session_destroy();
	exit("<script>alert('清空购物车成功');location.href='gw1.php';</script>");
	break;
}
?>
<?php
//购物车页面
error_reporting('E_ALL&~E_NOTICE');
header('content-type:text/html;charset-utf-8');
session_start();
//如果为空,证明没有物品在里面,,所以要去上一页买东西啦
if(empty($_SESSION['cart']))
{
	exit("<script>alert('请先购物物品');
		location.href='gw1.php';
		</script>");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<h1>购物车列表</h1>
	<table>
		<tr>
			<td>商品编号</td>
			<td>商品图片</td>
			<td>商品名称</td>
			<td>商品价格</td>
			<td>商品数量</td>
			<td>操作</td>
		</tr>

	<?php
	error_reporting('E_ALL&~E_NOTICE');
header('content-type:text/html;charset-utf-8');
		$sum=0;$i=1;
		foreach($_SESSION['cart'] as $data): {
		}//cart里面的东西,一个一个遍历给$data
	?>
	<tr><td><?php
	header("Content-Type: text/html;charset=utf-8");
				echo  $data['id'];
			?></td><td>
				<img src="<?php
					echo $data['pic'];
				?>"width='30' height='30'>
			</td>
			<td><?php
			header("Content-Type: text/html;charset=utf-8");
				echo $data['gname'];
			?></td>
			<td><?php
			header("Content-Type: text/html;charset=utf-8");
				echo  $data['price'];
			?></td>
			<td><?php
			header("Content-Type: text/html;charset=utf-8");
				echo $data['count'];
			?></td>
			<td>
				<a href="gw2.php?act=delGood&id=<?php
						echo $data['id'];

				?>">删除商品</a>
			</td>
		</tr>
		<?php
		header("Content-Type: text/html;charset=utf-8");
			$sum+=$data['price']*(int)$data['count'];endforeach;
		?>
		<tr colspan='6'>
			<td>
				合计<?php
				header("Content-Type: text/html;charset=utf-8");
					echo $sum;
				?>|<button>支付</button>|<a href="gw1.php">返回继续购物</a>
			</td>
		</tr>
		<tr><td>
				<a href="gw2.php?act=delCart&id=<?php
				header("Content-Type: text/html;charset=utf-8");
						echo $data['id'];

				?>">删除所有商品</a>
			</td></tr>
				</table>
	
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贵哥的编程之路(热爱分享)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值