php和mysql数据库制作简单的商城购物车(php怎么建立数据库mysql)

网友投稿 318 2022-07-21

今天整理了一下利用php和mysql数据库实现简单的购物车功能,主要用到的mysql表有以下几个:

login:

orders:

orderdetails:

fruit:

要制作商城,首先需要一个登陆页面:

代码如下:

       

   

点击“登录”按钮,跳转到登录页面的处理页面dengluchuli.php处理登录页面的信息:

 //连接数据库  $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"登录失败";

}

登录成功后,登录到商品界面,商品界面代码:

!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];

       }

   }

} ?>

   查看账户  

       查看购物车

   ?>    

商品页面展示:

点击“购买”,跳到add.php处理界面,将购买信息填入“购物车”,:

   !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=库存不足");

       }

   }     ?>

如点击“桔子”后面的购买,发生如下变化:

购物车代码:

!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(); ?>

   查看账户  

   查看购物车  

   ?>

点击“提交订单”,跳到订单处理页面dingdanchuli.php     将订单提交,删除订单信息,商品库存减少:

   //连接数据库    $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"); ?>

显示余额的页面:

代码:

   !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 ?>

   查看账户  

   查看购物车  

点击“提交订单”后,商品页面变化:

购物车页面清空,变化如下:

余额页面变化:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:php并发加锁问题分析与设计,可深入学习(php文件锁并发 性能)
下一篇:php常用字符串函数实例总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~