zoukankan      html  css  js  c++  java
  • 2018-2-26 php、mysql数据库制作简单的商城购物车

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

    login:

    orders:

    orderdetails:

    fruit:

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

    代码如下:

     1 <!doctype html>
     2 <html>
     3 <head>
     4 <meta charset="utf-8">
     5 <title>无标题文档</title>
     6 </head>
     7 
     8 <body>
     9     <form action="dengluchuli.php" method="post" >        
    10         <table >
    11             <tr>
    12                 <td>用户名:<input type="text" name="uid"></td>
    13                 
    14             </tr>
    15             <tr>
    16                 <td>密码:<input type="text" name="pwd"></td>
    17             </tr>
    18         </table>
    19         <input type="submit" value="登录"  id="denglu_div4">    
    20     </form>
    21 </body>
    22 </html>

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

     1  <?php
     2   session_start(); //开启session 必须要写到第一行
     3   $uid=$_POST["uid"];    //从登录页面获取到用户名和密码
     4   $pwd=$_POST["pwd"];
     5   //连接数据库
     6   $db=new MySQLi("localhost","root","","z_gwc");
     7   !mysqli_connect_error() or die("连接错误");
     8   $db->query("set names utf8");
     9   //查询密码
    10   $sql="select password from login where username='{$uid}'";
    11   $result=$db->Query($sql);
    12   $arr=$result->fetch_all();
    13   if($arr[0][0]==$pwd && !empty($pwd)) //判断所填写的密码和取到的密码是一样的,而且密码不能为空
    14   {
    15      //定义用户uid为超全局变量
    16      $_SESSION["uid"]=$uid;
    17      //跳转页面
    18      header("location:index1.php");
    19  }
    20  else
    21  {
    22      echo"登录失败";
    23  }

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

     1 <!--这是展示商品的页面-->
     2 <?php
     3 session_start();//开始
     4 //连接数据库
     5 $db=new MySQLi("localhost","root","","z_gwc");
     6 !mysqli_connect_error() or die("连接失败");
     7 $db->query("set names utf8");
     8 //获取传值
     9 $ids=$_GET["ids"];
    10 $uid=$_SESSION["uid"];    //用户账号
    11 //查询商品表
    12 $sql="select * from fruit";
    13 $res=$db->query($sql);
    14 $attr=$res->fetch_all();
    15 $sql="select Code from orders where UserName ='$uid'";
    16 $res=$db->query($sql);
    17 $dhattr=$res->fetch_all();//单号数组
    18 $dhStr="";
    19 //数组遍历,转为字符串
    20 foreach($dhattr as $v){
    21     $dhStr=$dhStr.$v[0]."','";
    22 }
    23 $dhStr=substr($dhStr,0,-3);//截取字符串
    24 $sql="select FruitCode,count(Count) from orderDetails where OrderCode in('$dhStr') group by FruitCode" ;
    25 $res=$db->query($sql);
    26 $spattr=$res->fetch_all();//购物车水果信息数组
    27 $strPice=0;
    28 foreach($attr as $v){
    29     foreach($spattr as $v1){
    30         if($v[0]==$v1[0]){
    31             $strPice=$strPice+$v[2]*$v1[1];
    32         }
    33     }
    34 }
    35 ?>
    36 <!doctype html>
    37 <html>
    38 <head>
    39 <meta charset="utf-8">
    40 <title>无标题文档</title>
    41 </head>
    42 
    43 <body>
    44 <a href="Login.php">登录</a>
    45 <h1>大苹果购物网</h1>
    46 <div>
    47     <a href="#">浏览商品</a>&nbsp;&nbsp;
    48     <a href="ViewAccount.php">查看账户</a>&nbsp;&nbsp;
    49     <!--将商品总价传到购物车页面-->
    50     <a href="ViewCart.php?strpice=<?php echo $strPice ?>&ids=<?php echo $ids ?>">查看购物车</a>
    51 </div>
    52 <div>
    53     购物车中有<span id="spnum"><?php echo count($spattr); ?></span>种商品,总价格:<span id="sppice"><?php echo $strPice; ?></span>元。
    54 </div>
    55 
    56 <table width="100%" border="1">
    57     <tr>
    58         <th>代号</th>
    59         <th>水果名称</th>
    60         <th>水果价格</th>
    61         <th>原产地</th>
    62         <th>货架</th>
    63         <th>库存量</th>
    64         <th>操作</th>
    65     </tr>
    66     <?php
    67         foreach($attr as $k=>$v){?>
    68             <tr>
    69                 <td><?php echo $v[0]; ?></td>
    70                 <td><?php echo $v[1]; ?></td>
    71                 <td><?php echo $v[2]; ?></td>
    72                 <td><?php echo $v[3]; ?></td>
    73                 <td><?php echo $v[4]; ?></td>
    74                 <td><?php echo $v[5]; ?></td>
    75                 <td><form action="add.php?uid=<?php echo $uid; ?>" method="post">
    76                     <input type="hidden" name="ids"
    77                     value="<?php echo $v[0]; ?>">
    78                     <button>购买</button>
    79                     
    80                 </form></td>
    81             </tr>
    82         <?php }
    83     ?>
    84     <span><?php echo $_GET["kc"] ?></span>
    85 </table>
    86 </body>
    87 </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>&nbsp;&nbsp;
        <a href="ViewAccount.php">查看账户</a>&nbsp;&nbsp;
        <a href="ViewCart.php">查看购物车</a>&nbsp;&nbsp;
    </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>&nbsp;&nbsp;
        <a href="ViewAccount.php">查看账户</a>&nbsp;&nbsp;
        <a href="ViewCart.php">查看购物车</a>&nbsp;&nbsp;
    </div>
    <span>您的账户中还剩余<?php echo  $dlattr[3]; ?>元。</span><br>
    <span style="color:red"><?php echo $jg ?></span>
    </body>
    </html>

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

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

    余额页面变化:

  • 相关阅读:
    开始使用ACE工作
    I AM NOTHING vs I AM SOMETHING
    After you have run the GIS Server Post Install
    李开复的勇气论
    爱到底是什么?
    办公室交际不能碰触的“地雷”
    近日比较忙顾不上写随笔了
    什么是爱情?什么是婚姻?
    给自己科普一下SOA、AOP、ORM
    成功者需要具备的素质
  • 原文地址:https://www.cnblogs.com/dns6/p/8476021.html
Copyright © 2011-2022 走看看