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>

 點擊“提交訂單”后,商品頁面變化:

購物車頁面清空,變化如下:

 

余額頁面變化:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM