今天整理了一下利用php和mysql数据库实现简单的购物车功能,主要用到的mysql表有以下几个:
login:
orderdetails:
fruit:
要制作商城,首先需要一个登陆页面:
代码如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <form action="dengluchuli.php" method="post" > <table > <tr> <td>用户名:<input type="text" name="uid"></td> </tr> <tr> <td>密码:<input type="text" name="pwd"></td> </tr> </table> <input type="submit" value="登录" id="denglu_div4"> </form> </body> </html>
点击“登录”按钮,跳转到登录页面的处理页面dengluchuli.php处理登录页面的信息:
<?php session_start(); //开启session 必须要写到第一行 $uid=$_POST["uid"]; //从登录页面获取到用户名和密码 $pwd=$_POST["pwd"]; //连接数据库 $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接错误"); $db->query("set names utf8"); //查询密码 $sql="select password from login where username='{$uid}'"; $result=$db->Query($sql); $arr=$result->fetch_all(); if($arr[0][0]==$pwd && !empty($pwd)) //判断所填写的密码和取到的密码是一样的,而且密码不能为空 { //定义用户uid为超全局变量 $_SESSION["uid"]=$uid; //跳转页面 header("location:index1.php"); } else { echo"登录失败"; }
登录成功后,登录到商品界面,商品界面代码:
<!--这是展示商品的页面--> <?php session_start();//开始 //连接数据库 $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); //获取传值 $ids=$_GET["ids"]; $uid=$_SESSION["uid"]; //用户账号 //查询商品表 $sql="select * from fruit"; $res=$db->query($sql); $attr=$res->fetch_all(); $sql="select Code from orders where UserName ='$uid'"; $res=$db->query($sql); $dhattr=$res->fetch_all();//单号数组 $dhStr=""; //数组遍历,转为字符串 foreach($dhattr as $v){ $dhStr=$dhStr.$v[0]."','"; } $dhStr=substr($dhStr,0,-3);//截取字符串 $sql="select FruitCode,count(Count) from orderDetails where OrderCode in('$dhStr') group by FruitCode" ; $res=$db->query($sql); $spattr=$res->fetch_all();//购物车水果信息数组 $strPice=0; foreach($attr as $v){ foreach($spattr as $v1){ if($v[0]==$v1[0]){ $strPice=$strPice+$v[2]*$v1[1]; } } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <body> <a href="Login.php">登录</a> <h1>大苹果购物网</h1> <div> <a href="#">浏览商品</a> <a href="ViewAccount.php">查看账户</a> <!--将商品总价传到购物车页面--> <a href="ViewCart.php?strpice=<?php echo $strPice ?>&ids=<?php echo $ids ?>">查看购物车</a> </div> <div> 购物车中有<span id="spnum"><?php echo count($spattr); ?></span>种商品,总价格:<span id="sppice"><?php echo $strPice; ?></span>元。 </div> <table width="100%" border="1"> <tr> <th>代号</th> <th>水果名称</th> <th>水果价格</th> <th>原产地</th> <th>货架</th> <th>库存量</th> <th>操作</th> </tr> <?php foreach($attr as $k=>$v){?> <tr> <td><?php echo $v[0]; ?></td> <td><?php echo $v[1]; ?></td> <td><?php echo $v[2]; ?></td> <td><?php echo $v[3]; ?></td> <td><?php echo $v[4]; ?></td> <td><?php echo $v[5]; ?></td> <td><form action="add.php?uid=<?php echo $uid; ?>" method="post"> <input type="hidden" name="ids" value="<?php echo $v[0]; ?>"> <button>购买</button> </form></td> </tr> <?php } ?> <span><?php echo $_GET["kc"] ?></span> </table> </body> </html>
商品页面展示:
点击“购买”,跳到add.php处理界面,将购买信息填入“购物车”,:
<?php session_start();//开始 //连接数据库 $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); //获取传值 $ids=$_POST["ids"]; $uid=$_SESSION["uid"]; $date=date("Y-m-d h:i:s");//获取时间 $sql="select numbers from fruit where ids='$ids'"; $res=$db->query($sql); $att=$res->fetch_row(); foreach($att as $v){ if($v>0){ //条件判断 $sql="insert into orders values('$uid"."$date','$uid','$date')"; $db->query($sql); $sql="insert into orderdetails values('','$uid"."$date','$ids',1)"; $db->query($sql); header("location:index1.php?ids=$ids"); }else{ header("location:index1.php?kc=库存不足"); } } ?>
如点击“桔子”后面的购买,发生如下变化:
此时购物车页面:
购物车代码:
<!--这是购物车页面--> <?php session_start();//开始 //连接数据库 $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); $strpice=$_GET["strpice"];//接收从index.php传过来的商品总价 $ids=$_GET["ids"]; $dlStr=$_SESSION["dlStr"];//超全局 //查询数据 $sql="select a.ids,". "a.ordercode,". "b.name,". "b.price,". "count(a.count) ". "from orderdetails as a ". "join fruit as b ". "on a.fruitcode=b.ids group by b.name;"; $res=$db->query($sql); $spattr=$res->fetch_all(); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>购物车</title> </head> <body> <a href="Login.php">登录</a> <h1>大苹果购物网</h1> <div> <a href="index1.php">浏览商品</a> <a href="ViewAccount.php">查看账户</a> <a href="ViewCart.php">查看购物车</a> </div> <table width="100%" border="1"> <tr> <th>商品名称</th> <th>商品单价</th> <th>购买数量</th> <th>操作</th> </tr> <?php foreach($spattr as $v){ ?> <tr> <td><?php echo $v[2]; ?></td> <td><?php echo $v[3]; ?></td> <td><?php echo $v[4]; ?></td> <td><a href="../gouwuchegai/adf.php?id=<?php echo 1234 ?>"></a> <form action="delchuli.php?name=<?php echo $v[2]; ?>" method="post"> <input type="hidden" name="orderCode" value="<?php echo $v[1]; ?>"> <button>删除</button> </form> </td> </tr> <?php } ?> </table> <a href="dingdanchuli.php?strpice=<?php echo $strpice ?>&ids=<?php echo $ids ?>">提交订单</a> </body> </html>
点击“提交订单”,跳到订单处理页面dingdanchuli.php 将订单提交,删除订单信息,商品库存减少:
<?php session_start(); //连接数据库 $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); $uid=$_SESSION["uid"];//获取超全局变量uid $strpice=$_GET["strpice"];//这是商品传过来的总价 $ids=$_GET["ids"]; $dlStr=$_SESSION["dlStr"];//余额 /*sql语句查询订单号*/ $sql="select code from orders where username='$uid'"; $res=$db->query($sql); $codstr=$res->fetch_all(); $jg=""; if($dlStr>=$strpice){ $jg="提交成功"; foreach($codstr as $v){ $sql="update login set account =account-$strpice where username='$uid'"; $db->query($sql); $sql="update fruit set numbers=numbers-1 where ids='$ids'"; $db->query($sql); //删除orders表中内容 $sql="delete from orders where code='$v[0]'"; $db->query($sql); //删除orderdetails表中的内容 $sql="delete from orderdetails where ordercode='$v[0]'"; $db->query($sql); } }else{ $jg="余额不足"; } //跳转页面 header("location:ViewAccount.php?jg=$jg"); ?>
显示余额的页面:
代码:
<!--这个页面显示账户余额--> <?php session_start();//开始 $jg=$_GET["jg"];//获取从dingdanchuli.php接收的结果值 $uid=$_SESSION["uid"];//超全局变量uid /*连接数据库*/ $db=new MySQLi("localhost","root","","z_gwc"); !mysqli_connect_error() or die("连接失败"); $db->query("set names utf8"); /*sql语句查询余额*/ $sql="select * from login where username='$uid'"; $res=$db->query($sql);//执行sql语句 $dlattr=$res->fetch_row();//获取一维数组结果集 $_SESSION["dlStr"]=$dlattr[3];//设置全局变量余额dhStr ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>余额</title> </head> <body> <a href="Login.php">登录</a> <h1>大苹果购物网</h1> <div> <a href="index1.php">浏览商品</a> <a href="ViewAccount.php">查看账户</a> <a href="ViewCart.php">查看购物车</a> </div> <span>您的账户中还剩余<?php echo $dlattr[3]; ?>元。</span><br> <span style="color:red"><?php echo $jg ?></span> </body> </html>
点击“提交订单”后,商品页面变化:
购物车页面清空,变化如下:
余额页面变化:
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/6510/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料