• 购物车练习


    登陆页面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="../../class/jquery-1.11.2.min.js"></script>
    
    <title>无标题文档</title>
    </head>
    
    <body>
    <div>用户名:<input type="text" id="uid" /></div>
    <div>密码:<input type="text" id="pwd" /></div>
    <input type="button" value="登陆" id="btn" />
    
    <script type="text/javascript">
    $("#btn").click(function(){
        var uid = $("#uid").val();
        var pwd = $("#pwd").val();
        
        $.ajax({
            url:"loginchuli.php",
            data:{u:uid,p:pwd},
            type:"POST",
            dataType:"TEXT",
            success: function(data){
                if(data.trim()=="OK"){
                    window.location.href="main.PHP";
                    }else{
                    alert("用户名或密码错误");
                    }
                }
            })
        })
    </script>
    </body>
    </html>
    登陆

    登陆处理

     1 <?php
     2 session_start();
     3 include("../../class/dbda.class.php");
     4 $db = new DBDA();
     5 
     6 $uid = $_POST["u"];
     7 $pwd = $_POST["p"];
     8 
     9 $sql = "select password from login where username='{$uid}'";
    10 $mm = $db->StrQuery($sql);
    11 if($mm == $pwd && $pwd != ""){
    12     $_SESSION["uid"]=$uid;
    13     echo "OK";
    14     }else{
    15     echo "NO";
    16     }
    登陆处理

    主页面

     1 <?php
     2 session_start();
     3 include("../../class/dbda.class.php");
     4 $db = new DBDA();
     5 ?>
     6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     7 <html xmlns="http://www.w3.org/1999/xhtml">
     8 <head>
     9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    10 <title>无标题文档</title>
    11 <style type="text/css">
    12 .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    13 </style>
    14 </head>
    15 
    16 <body>
    17 
    18 <div style="100%; height:100px; background-color:#06F">
    19     <h1 style="float:left">大苹果购物网</h1>
    20     <a href="zhuxiao.php" style="float:right; margin-top:40px">注销</a>
    21 </div><br />
    22 <div style="100%; height:600px">
    23     <div id="left" style="20%; float:left">
    24         <a href="main.php"><div class="list">浏览商品</div></a>
    25         <a href=""><div class="list">查看账户</div></a>
    26         <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    27     </div>
    28     <div id="right" style="80%; float:left">
    29         <?php
    30         $agwc = array();
    31         if(!empty($_SESSION["gwc"])){
    32             $agwc = $_SESSION["gwc"];
    33             }
    34         $zhonglei = count($agwc);
    35         $sum = 0;
    36         foreach($agwc as $v){
    37             $sql = "select price from fruit where ids='{$v[0]}'";
    38             $danjia = $db->StrQuery($sql);
    39             $sum = $sum +$danjia*$v[1];
    40             
    41             }
    42         echo "<div>购物车中有:{$zhonglei}种商品,总价为{$sum}元</div>";
    43         
    44         ?>
    45         <table width="100%" border="1" cellpadding="0" cellspacing="0">
    46         <tr>
    47             <td>代号</td>
    48             <td>水果名称</td>
    49             <td>水果价格</td>
    50             <td>原产地</td>
    51             <td>库存量</td>
    52             <td>操作</td>
    53         </tr>
    54         <?php
    55         
    56         $sql = "select * from fruit";
    57         $attr = $db->Query($sql);
    58         
    59         foreach($attr as $v){
    60             echo "<tr>
    61             <td>{$v[0]}</td>
    62             <td>{$v[1]}</td>
    63             <td>{$v[2]}</td>
    64             <td>{$v[3]}</td>
    65             <td>{$v[4]}</td>
    66             <td><a href='goumai.php?code={$v[0]}'>购买</a></td>
    67         </tr>";
    68             
    69             }
    70         
    71         ?>
    72         
    73         
    74         </table>
    75     
    76     </div>
    77 </div>
    78 </body>
    79 </html>
    主页面

    购买处理

     1 <?php
     2 session_start();
     3 $code = $_GET["code"];
     4 
     5 if(empty($_SESSION["gwc"])){
     6     //第一次点击购买
     7     $attr = array(
     8         array($code,1)
     9     );
    10     $_SESSION["gwc"] = $attr;
    11     
    12     }else{
    13     //不是第一次购买
    14     $attr = $_SESSION["gwc"];
    15     $bs = 0;
    16     foreach($attr as $k=>$v){
    17         if($v[0]==$code){
    18             $bs=1;
    19             $attr[$k][1] = $attr[$k][1]+1;
    20             
    21             }
    22         }
    23     //如果没有在数组里面出现
    24     if($bs==0){
    25         $shuzu = array($code,1);
    26         $attr[] = $shuzu;
    27         }
    28     $_SESSION["gwc"]=$attr;
    29     }
    30 header("location:main.php");
    购买处理

    购物车页面

     1 <?php
     2 session_start();
     3 ?>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     8 <script src="../../class/jquery-1.11.2.min.js"></script>
     9 <title>无标题文档</title>
    10 <style type="text/css">
    11 .list{ 100%; height:30px; margin-top:10px; text-align:center; line-height:30px; vertical-align:middle}
    12 </style>
    13 </head>
    14 
    15 <body>
    16 <div style="100%; height:100px; background-color:#06F">
    17     <h1 style="float:left">大苹果购物网</h1>
    18     <a href="zhuxiao.php" style="float:right; margin-top:40px">注销</a>
    19 </div><br />
    20 <div style="100%; height:600px">
    21     <div id="left" style="20%; float:left">
    22         <a href="main.php"><div class="list">浏览商品</div></a>
    23         <a href=""><div class="list">查看账户</div></a>
    24         <a href="gouwuche.php"><div class="list">查看购物车</div></a>
    25     </div>
    26     <div id="right" style="80%; float:left">
    27         <table width="100%" border="1" cellpadding="0" cellspacing="0">
    28         <tr>
    29             <td>商品名称</td>
    30             <td>商品单价</td>
    31             <td>购买数量</td>
    32             <td>操作</td>
    33         </tr>
    34         <?php
    35         include("../../class/dbda.class.php");
    36         $db = new DBDA();
    37         $attr = array();
    38         if(!empty($_SESSION["gwc"])){
    39             $attr = $_SESSION["gwc"];
    40             }
    41         foreach($attr as $k=>$v){
    42             $sql = "select name,price from fruit where ids='{$v[0]}'";
    43             $ashuiguo = $db->Query($sql);
    44             
    45             echo "<tr><td>{$ashuiguo[0][0]}</td><td>{$ashuiguo[0][1]}</td><td>{$v[1]}</td><td><a href='shanchu.php?sy={$k}'>删除</a></td></tr>";
    46             }    
    47         
    48         ?>
    49         
    50         </table>
    51         <div id="tj">提交订单</div><div id="ts"></div>
    52     
    53     </div>
    54 </div>
    55 
    56 <script type="text/javascript">
    57 $("#tj").click(function(){
    58     $.ajax({
    59         url:"dingdan.php",
    60         dataType:"TEXT",
    61         success: function(data){
    62             if(data.trim()=="OK"){
    63                 alert("购买成功");
    64                 }else if(data.trim()=="YEBUZU"){
    65                 $("#ts").html("余额不足");
    66                 $("#ts").css("color","red");
    67                 }else{
    68                 $("#ts").html(data);
    69                 $("#ts").css("color","red");
    70                 }
    71             }
    72         
    73         })
    74     })
    75 </script>
    76 </body>
    77 </html>
    购物车

    删除

     1 <?php
     2 session_start();
     3 $sy = $_GET["sy"];
     4 
     5 $attr = $_SESSION["gwc"];
     6 
     7 if($attr[$sy][1]>1){
     8     $attr[$sy][1] = $attr[$sy][1]-1;
     9     }else{
    10     unset($attr[$sy]);
    11     $attr = array_values($attr);
    12     }
    13 $_SESSION["gwc"] = $attr;
    14 
    15 header("location:gouwuche.php");
    删除

    订单处理

     1 <?php
     2 session_start();
     3 include("../../class/dbda.class.php");
     4 $db = new DBDA();
     5 $uid = $_SESSION["uid"];
     6 $attr = array();
     7 if(!empty($_SESSION["gwc"])){
     8     $attr = $_SESSION["gwc"];
     9     }
    10 //看两个条件是否都满足
    11 $bs = true;
    12     
    13 //判断余额是否满足
    14 $syue = "select account from login where username='{$uid}'";
    15 $yue = $db->StrQuery($syue);
    16 
    17 $sum = 0;
    18 foreach($attr as $v){
    19     $sql = "select price from fruit where ids='{$v[0]}'";
    20     $danjia = $db->StrQuery($sql);
    21     $sum = $sum +$danjia*$v[1];
    22     }
    23 
    24 if($yue<$sum){
    25     $bs = false;
    26     echo "YEBUZU";
    27     exit;
    28     }
    29     
    30 //判读库存是否满足
    31 foreach($attr as $v){
    32     $skucun = "select name,numbers from fruit where ids='{$v[0]}'";
    33     $akucun = $db->Query($skucun);
    34     if($akucun[0][1]<$v[1]){
    35         $bs = false;
    36         echo "{$akucun[0][0]}库存不足!";
    37         exit;
    38         }
    39     }
    40 
    41 
    42 //添加订单,减库存,减余额
    43 if($bs){
    44     //减库存
    45     foreach($attr as $v){
    46         $sql = "updata fruit set numbers = numbers-{$v[1]} where ids='{$v[0]}'";
    47         $db->Query($sql,0);
    48         }
    49     
    50     //减余额
    51     $jianyue = "updata login set account=account-{$sum} where username='{$uid}'";
    52     $db->Query($jianyue,0);
    53     //添加订单
    54     $dingdanhao = $uid+date("YmdHis");
    55     $t = time();
    56     $sorder = "insert into orders values('{$dingdanhao}','{$uid}','{$t}')";
    57     $db->Query($sorder,0);
    58     
    59     foreach($attr as $v){
    60         $sxq = "insert into orderdetails values('','{$dingdanhao}','{$v[0]}','{$v[1]}')";
    61         $db->Query($sxq,0);
    62         }
    63     
    64     }
    65     echo "OK";
    订单处理
  • 相关阅读:
    Python 操作 MySQL数据库提示pymysql.err.InternalError: (1054, "Unknown column 'XXXXXXXXX' in 'where clause'")解决方法
    MySQL连接池不能查到刚写入的数据——连接池配置问题
    python 将字典转为bytes类型字典
    关于状态机的问题思考——什么时候达到新的状态?什么时候清除老状态?新状态与老状态之间的关系
    mysql 8.0.19 安装 及 端口修改
    sprintf printf 输出数据固定格式——数字前补零
    思维大爆炸
    IO点作为状态判断——一定要做软件“消抖”
    React-umi-request动态刷新Token功能实现及node.js 代码逻辑
    js测试题
  • 原文地址:https://www.cnblogs.com/The-second/p/6065125.html
Copyright © 2020-2023  润新知