主頁 > 後端開發 > 電子商城專案開發(前臺功能模塊開發)

電子商城專案開發(前臺功能模塊開發)

2020-09-20 06:45:22 後端開發

接著之前那篇 “電子商城專案開發(后臺功能模塊開發)”

登陸頁面login.php

    <?php
    require '../tools.func.php';
    require '../db.func.php';
    // 判斷當前是否為post提交
    if (!empty($_POST['username'])) {
        $action = htmlentities($_GET['action']);
      $prefix = getDBPrefix();
        if ($action == 'reg') {
            $username = htmlentities($_POST['username']);
            $password = md5(htmlentities($_POST['password']));
            $email = htmlentities($_POST['email']);

            $created_at = date('Y-m-d H:i:s');
            $sql = "INSERT INTO {$prefix}user(username, password, email, created_at)
                            VALUES('$username', '$password', '$email', '$created_at')";
            if (execute($sql)) {
                setInfo('注冊成功');
            } else {
                setInfo('注冊失敗');
            }
        } elseif ($action == 'login') {
            $username = htmlentities($_POST['username']);
            $password = md5(htmlentities($_POST['password']));
            $sql = "SELECT id, username 
                            FROM {$prefix}user
                            WHERE username = '$username' AND password = '$password'";
            $res = queryOne($sql);
            if ($res) {
                setSession('shop', ['username' => $username, 'id' => $res['id']]);
                header('location: index.php');
            } else {
                setInfo('用戶名或者密碼錯誤');
            }
        }
    }
    // 判斷操作為login 還是 reg
    // 如果是reg,要接收post資料,插入新資料
    // 如果是login,要查詢資料庫,判斷用戶名或者密碼是否正確,正確的話寫入session
    // 顯示結果

    require 'header.php';
    ?>
        <!-- Start Login Register Area -->
        <div class="htc__login__register bg__white ptb--130">
            <div class="container">
                <div class="row">
                    <div class="col-md-6 col-md-offset-3">
                        <ul class="login__register__menu" role="tablist">
                            <li role="presentation" class="login active"><a href="#login" role="tab" data-toggle="tab">登錄</a></li>
                            <li role="presentation" class="register"><a href="#register" role="tab" data-toggle="tab">注冊</a></li>
                        </ul>
                    </div>
                </div>
                <!-- Start Login Register Content -->
                <div class="row">
                    <div class="col-md-6 col-md-offset-3">
                        <div class="htc__login__register__wrap">
                            <!-- Start Single Content -->
                            <div id="login" role="tabpanel" class="single__tabs__panel tab-pane fade in active">
                                <p><?php if (hasInfo()) echo getInfo(); ?></p>
                                <form id="loginform" class="login" method="post" action="login.php?action=login">
                                    <input type="text" name="username" placeholder="User Name*">
                                    <input type="password" name="password" placeholder="Password*">
                                </form>

                                <div class="htc__login__btn mt--30">
                                    <a href="javascript:document.getElementById('loginform').submit();">登錄</a>
                                </div>

                            </div>
                            <!-- End Single Content -->
                            <!-- Start Single Content -->
                            <div id="register" role="tabpanel" class="single__tabs__panel tab-pane fade">
                                <p><?php if (hasInfo()) echo getInfo(); ?></p>
                                <form id="regform" class="login" action="login.php?action=reg" method="post">
                                    <input type="text" name="username" placeholder="Name*">
                                    <input type="email" name="email" placeholder="Email*">
                                    <input type="password" name="password" placeholder="Password*">
                                </form>

                                <div class="htc__login__btn">
                                    <a href="javascript:document.getElementById('regform').submit();">注冊</a>
                                </div>

                            </div>
                            <!-- End Single Content -->
                        </div>
                    </div>
                </div>
                <!-- End Login Register Content -->
            </div>
        </div>
        <!-- End Login Register Area -->

        <div class="only-banner ptb--10 bg__white">
        </div>
    <?php
    require 'footer.php';
    ?>

header.php

    <?php
    $username = getSession('username', 'shop');
    $cart_header = [];
    if (!empty($username)) {
        $uid = getSession('id', 'shop');
        // 從資料表查詢購物車資料
        $prefix = getDBPrefix();
        $sql = "SELECT id, price, quantity, products 
                        FROM {$prefix}cart WHERE uid = '$uid'";
        $cart_header = queryOne($sql);
        $cart_header['products'] = json_decode($cart_header['products'], true);
    } else {
        // 從session查詢購物車資料
      $cart_header = getSession('cart', 'shop');
    }
    ?>
    <!doctype html>
    <html class="no-js" lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title>慕課商城</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- Place favicon.ico in the root directory -->
        <link rel="apple-touch-icon" href="apple-touch-icon.png">


        <!-- All css files are included here. -->
        <!-- Bootstrap fremwork main css -->
        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
        <!-- Owl Carousel main css -->
        <link rel="stylesheet" href="assets/css/owl.carousel.min.css">
        <link rel="stylesheet" href="assets/css/owl.theme.default.min.css">
        <!-- This core.css file contents all plugings css file. -->
        <link rel="stylesheet" href="assets/css/core.css">
        <!-- Theme shortcodes/elements style -->
        <link rel="stylesheet" href="assets/css/shortcode/shortcodes.css">
        <!-- Theme main style -->
        <link rel="stylesheet" href="assets/css/style.css">
        <!-- Responsive css -->
        <link rel="stylesheet" href="assets/css/responsive.css">
        <!-- User style -->
        <link rel="stylesheet" href="assets/css/custom.css">


        <!-- Modernizr JS -->
        <script src="assets/js/vendor/modernizr-2.8.3.min.js"></script>
    </head>

    <body>
    <!-- Body main wrapper start -->
    <div class="wrapper fixed__footer">
        <!-- Start Header Style -->
        <header id="header" class="htc-header header--3 bg__white">
            <!-- Start Mainmenu Area -->
            <div id="sticky-header-with-topbar" class="mainmenu__area sticky__header">
                <div class="container">
                    <div class="row">
                        <div class="col-md-2 col-lg-2 col-sm-3 col-xs-3">
                            <div class="logo">
                                <a href="index.php">
                                    <img src="assets/images/logo/logo.png" alt="logo">
                                </a>
                            </div>
                        </div>
                        <!-- Start MAinmenu Ares -->
                        <div class="col-md-8 col-lg-8 col-sm-6 col-xs-6">
                            <nav class="mainmenu__nav hidden-xs hidden-sm">
                                <ul class="main__menu">
                                    <li><a href="index.php">首頁</a></li>
                                </ul>
                            </nav>

                        </div>
                        <!-- End MAinmenu Ares -->
                        <div class="col-md-4 col-sm-4 col-xs-3">
                            <ul class="menu-extra">
                                <li><a href="login.php"><span class="ti-user"></span> </a>
                                    <?php if (empty($username)): ?>
                                    <a href="login.php">注冊</a>
                                    <?php else: ?>
                                    歡迎回來, <?php echo $username; ?> <a href="logout.php">退出</a>
                                    <?php endif; ?>
                                </li>
                                <li class="cart__menu"><span class="ti-shopping-cart"></span></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End Mainmenu Area -->
        </header>
        <!-- End Header Style -->

        <div class="body__overlay"></div>
        <!-- Start Offset Wrapper -->
        <div class="offset__wrapper">
            <!-- Start Offset MEnu -->
            <div class="offsetmenu">
                <div class="offsetmenu__inner">
                    <div class="offsetmenu__close__btn">
                        <a href="#"><i class="zmdi zmdi-close"></i></a>
                    </div>
                    <div class="off__contact">
                        <div class="logo">
                            <a href="index.php">
                                <img src="assets/images/logo/logo.png" alt="logo">
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End Offset MEnu -->
            <!-- Start Cart Panel -->
            <div class="shopping__cart">
                <div class="shopping__cart__inner">
                    <div class="offsetmenu__close__btn">
                        <a href="#"><i class="zmdi zmdi-close"></i></a>
                    </div>
                    <div class="shp__cart__wrap">
                        <?php if (! empty($cart_header)): ?>
                        <?php foreach ($cart_header['products'] as $pid => $header_pro): ?>
                        <div class="shp__single__product">
                            <div class="shp__pro__thumb">
                                <a href="#">
                                    <img src="assets/uploads/default.jpeg" alt="product images">
                                </a>
                            </div>
                            <div class="shp__pro__details">
                                <h2><a href="product_details.php?id=<?php echo $pid; ?>"></a><?php echo $header_pro['product']['name']; ?></h2>
                                <span class="quantity">數量: <?php echo $header_pro['quantity']; ?></span>
                                <span class="shp__price"><?php echo $header_pro['product']['price']; ?></span>
                            </div>
                            <div class="remove__btn">
                                <a href="#" title="Remove this item"><i class="zmdi zmdi-close"></i></a>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <ul class="shoping__total">
                        <li class="subtotal">總計:</li>
                        <li class="total__price"><?php echo $cart_header['price']; ?></li>
                    </ul>
                    <?php endif; ?>
                    <ul class="shopping__btn">
                        <li><a href="cart.php">查看購物車</a></li>
                    </ul>
                </div>
            </div>
            <!-- End Cart Panel -->
        </div>
        <!-- End Offset Wrapper -->

footer.php

    <!-- Start Footer Area -->
    <footer class="htc__foooter__area gray-bg">
        <div class="container">

            <!-- Start Copyright Area -->
            <div class="htc__copyright__area">
                <div class="row">
                    <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                        <div class="copyright__inner">
                            <ul class="footer__menu">
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <!-- End Copyright Area -->
        </div>
    </footer>
    <!-- End Footer Area -->
    </div>
    <!-- Body main wrapper end -->
    <!-- Placed js at the end of the document so the pages load faster -->

    <!-- jquery latest version -->
    <script src="assets/js/vendor/jquery-1.12.0.min.js"></script>
    <!-- Bootstrap framework js -->
    <script src="assets/js/bootstrap.min.js"></script>
    <!-- All js plugins included in this file. -->
    <script src="assets/js/plugins.js"></script>
    <script src="assets/js/slick.min.js"></script>
    <script src="assets/js/owl.carousel.min.js"></script>
    <!-- Waypoints.min.js. -->
    <script src="assets/js/waypoints.min.js"></script>
    <!-- Main js file that contents all jQuery plugins activation. -->
    <script src="assets/js/main.js"></script>

    </body>

    </html>

登陸后進入首頁index.php

    <?php
    require '../tools.func.php';
    require '../db.func.php';
    // 1. 查詢所有商品 imooc_product
    // 2. 寫sql陳述句
    $prefix = getDBPrefix();
    $sql = "SELECT id,name,price FROM {$prefix}product ORDER BY created_at DESC";
    $data = https://www.cnblogs.com/chenyingying0/p/query($sql);
    // 3. 遍歷結果

    require'header.php';
    ?>
    <!-- Start Our Product Area -->
    <section class="htc__product__area bg__white">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div class="product-categories-all">
                        <div class="product-categories-title" style="border-bottom: 1px solid rgba(129, 129, 129, 0.2)">
                            <h3>所有商品</h3>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <?php foreach ($data as $product): ?>
                <div class="col-md-3 single__pro col-lg-3 cat--1 col-sm-4 col-xs-12">
                    <div style="margin-top: 20px" class="product foo">
                        <div class="product__inner">
                            <div class="pro__thumb">
                                <a href="product_details.php?id=<?php echo $product['id']; ?>">
                                    <img src="assets/uploads/default.jpeg" alt="product images">
                                </a>
                            </div>
                            <div class="product__hover__info">
                                <ul class="product__action">
                                    <li><a title="加入購物車" href="cart_add.php?product_id=<?php echo $product['id']; ?>&quantity=1"><span class="ti-shopping-cart"></span></a></li>
                                    <li><a title="查看詳情" href="product_details.php?id=<?php echo $product['id']; ?>">查看詳情</a></li>
                                </ul>
                            </div>
                        </div>
                        <div class="product__details">
                            <h2 style="margin-left: 20px;text-align: center;"><a href="product_details.php?id=<?php echo $product['id']; ?>"><?php echo $product['name']; ?></a></h2>
                            <ul class="product__price">
                                <li class="new__price" style="margin: 0 auto;"><?php echo $product['price']; ?></li>
                            </ul>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </section>
    <!-- End Our Product Area -->
    <div class="only-banner ptb--10 bg__white">
    </div>
    <?php
    require 'footer.php';
    ?>

登出頁面logout.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 12:32
     */

    require '../tools.func.php';
    deleteSession('shop');

    header('location: index.php');

商品詳情頁product_details.php

    <?php
    require '../tools.func.php';
    require '../db.func.php';
    // 1. 接收get id
    $id = intval($_GET['id']);
    // 2. 判斷 get id,如果id不存,跳回到首頁
    if (empty($id)) {
        header('location: index.php');
    }
    // 3. 根據ID 查詢對應的商品
    $prefix = getDBPrefix();
    $sql = "SELECT id, name, price, description
                    FROM {$prefix}product WHERE id = '$id'";
    $current_product = queryOne($sql);
    // 4. 將資料寫入到頁面當中
    require 'header.php';
    ?>
    <!-- Start Product Details -->
    <section class="htc__product__details pt--120 pb--100 bg__white">
        <div class="container">
            <div class="row">
                <div class="col-md-4 col-lg-4 col-sm-12 col-xs-12">
                    <div class="product__details__container">
                        <div class="product__big__images" style="width: 100%;max-width: 100%">
                            <div class="portfolio-full-image tab-content">
                                <div role="tabpanel" class="tab-pane fade in active">
                                    <img width="100%" src="assets/uploads/default.jpeg" alt="full-image">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-8 col-lg-8 col-sm-12 col-xs-12 smt-30 xmt-30">
                    <div class="htc__product__details__inner">
                        <div class="pro__detl__title">
                            <h2><?php echo $current_product['name']; ?></h2>
                        </div>
                        <div class="pro__details">
                            <p><?php echo mb_substr($current_product['description'], 0, 50, 'utf-8') . '...'; ?></p>
                        </div>
                        <ul class="pro__dtl__prize">
                            <li><?php echo $current_product['price']; ?></li>
                        </ul>
                        <div class="product-action-wrap">
                            <div class="prodict-statas"><span>數量 :</span></div>
                            <div class="product-quantity">
                                <form id='myform' method='GET' action='cart_add.php'>
                                    <input type="hidden" name="product_id" value="<?php echo $id; ?>">
                                    <div class="product-quantity">
                                        <div class="cart-plus-minus">
                                            <input class="cart-plus-minus-box" type="text" name="quantity" value="1">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <ul class="pro__dtl__btn">
                            <li class="buy__now__btn"><a href="javascript:document.getElementById('myform').submit();">加入購物車</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- End Product Details -->
    <!-- Start Product tab -->
    <section class="htc__product__details__tab bg__white pb--120">
        <div class="container">
            <div class="row">
                <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">
                    <ul class="product__deatils__tab mb--60" role="tablist">
                        <li role="presentation" class="active">
                            <a href="#description" role="tab" data-toggle="tab">商品描述</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="product__details__tab__content">
                        <!-- Start Single Content -->
                        <div role="tabpanel" id="description" class="product__tab__content fade in active">
                            <div class="product__description__wrap">
                                <div class="product__desc">
                                    <h2 class="title__6">詳情</h2>
                                    <p><?php echo $current_product['description']; ?></p>
                                </div>
                            </div>
                        </div>
                        <!-- End Single Content -->
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- End Product tab -->
    <div class="only-banner ptb--10 bg__white">
    </div>
    <?php
    require 'footer.php';
    ?>

購物車頁面cart.php

    <?php
    require '../tools.func.php';
    require '../db.func.php';
    require 'auth.php';
    // 1. 從資料表當中查詢購物車資料
    $uid = getSession('id', 'shop');
    $prefix = getDBPrefix();
    $sql = "SELECT id,price,products,quantity 
                    FROM {$prefix}cart WHERE uid = '$uid'";
    $cart_page_data = https://www.cnblogs.com/chenyingying0/p/queryOne($sql);
    $cart_page_data['products'] = json_decode($cart_page_data['products'], true);
    // 2. 遍歷資料到頁面


    require 'header.php';
    ?>
    <div class="cart-main-area bg__white">
        <div class="container">
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <?php if (hasInfo()) echo getInfo(); ?>
                    <form action="#">
                        <div class="table-content table-responsive">
                            <table>
                                <thead>
                                <tr>
                                    <th class="product-thumbnail"></th>
                                    <th class="product-name">商品名稱</th>
                                    <th class="product-price">單價</th>
                                    <th class="product-quantity">數量</th>
                                    <th class="product-subtotal">總計</th>
                                    <th class="product-remove">編輯</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php if (!empty($cart_page_data)): ?>
                                <?php foreach ($cart_page_data['products'] as $pid => $cart_product): ?>
                                <tr>
                                    <td class="product-thumbnail">
                                        <a href="product_details.php?id=<?php echo $pid ?>">
                                            <img src="assets/uploads/default.jpeg" alt="product img"/>
                                        </a>
                                    </td>
                                    <td class="product-name">
                                        <a href="product_details.php?id=<?php echo $pid ?>">
                                            <?php echo $cart_product['product']['name']; ?>
                                        </a>
                                    </td>
                                    <td class="product-price">
                                        <span class="amount"><?php echo $cart_product['product']['price']; ?></span>
                                    </td>
                                    <td class="product-quantity">
                                        <input type="number" disabled value="<?php echo $cart_product['quantity']; ?>"/>
                                    </td>
                                    <td class="product-subtotal"><?php echo $cart_product['product']['price'] * $cart_product['quantity']; ?></td>
                                    <td class="product-remove"><a href="cart_del.php?product_id=<?php echo $pid; ?>">X</a></td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="row">
                            <div class="col-md-8 col-sm-7 col-xs-12">
                                <div class="buttons-cart">
                                    <a href="index.php">繼續購物</a>
                                </div>

                            </div>
                            <div class="col-md-4 col-sm-5 col-xs-12">
                                <div class="cart_totals">
                                    <table>
                                        <tbody>
                                        <tr class="cart-subtotal">
                                            <th>小計</th>
                                            <td><span class="amount"><?php echo isset($cart_page_data['price']) ? $cart_page_data['price'] : '0.0' ?></span></td>
                                        </tr>
                                        <tr class="shipping">
                                            <th>快遞</th>
                                            <td>
                                                <ul id="shipping_method">
                                                    <li>
                                                        <input type="radio" checked/>
                                                        <label>
                                                            包郵
                                                        </label>
                                                    </li>
                                                    <li></li>
                                                </ul>
                                            </td>
                                        </tr>
                                        <tr class="order-total">
                                            <th>總價</th>
                                            <td>
                                                <strong><span class="amount"><?php echo isset($cart_page_data['price']) ? $cart_page_data['price'] : '0.0' ?></span></strong>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>

                                    <div class="wc-proceed-to-checkout" style="clear: both;">
                                        <a href="checkout.php">去付款</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="only-banner ptb--10 bg__white">
    </div>
    <?php
    require 'footer.php';
    ?>

查看購物車前確認登陸狀態auth.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 14:47
     */
    if (empty(getSession('username', 'shop'))) {
        header('location: login.php');
        exit;
    }

增加商品添加到購物車cart_add.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 13:18
     */
    require '../tools.func.php';
    require '../db.func.php';

    // 1. 接收商品 id
    $product_id = intval($_GET['product_id']);
    // 2. 接收數量
    $quantity = intval($_GET['quantity']);

    if (empty($product_id) || empty($quantity)) {
        header('location: index.php');
        exit;
    }

    $prefix = getDBPrefix();
    $sql = "SELECT id, name, price, code, description
                FROM {$prefix}product WHERE id = '$product_id'";
    $product = queryOne($sql);

    // 3. 判斷當前用戶是否已經登錄
    if (!empty(getSession('username', 'shop'))) {
        // 如果已經登錄,將購物車資料存入到 資料表(
        // id,
        // products[
        //  2 => [quantity => 4, product => [id => ,price =>,]],
        //  3 => [quantity => 2, product => []],
        //  4 =>
        //  ],
        // price, quantity, uid)
        $uid = getSession('id', 'shop');

        $sql = "SELECT id,products,price,quantity,uid 
                FROM {$prefix}cart WHERE uid = '$uid'";
        $cart = queryOne($sql);
        if (!empty($cart)) {
            // 更新記錄
            $products = json_decode($cart['products'], true);
            if (array_key_exists($product_id, $products)) {
                $products[$product_id]['quantity'] += $quantity;
            } else {
                $products[$product_id] = ['quantity' => $quantity, 'product' => $product];
            }
            $products = addslashes(json_encode($products));
            $cart_price = $cart['price'] + $quantity * $product['price'];
            $cart_quantity = $cart['quantity'] + $quantity;
            $sql = "UPDATE {$prefix}cart 
                    SET products = '$products', price = '$cart_price', quantity = '$cart_quantity'
                    WHERE id = '{$cart['id']}'";
            execute($sql);
        } else {
            // 生成 products, price, quantity, uid, created_at
            $products[$product_id] = ['quantity' => $quantity, 'product' => $product];
            $price = $product['price'] * $quantity;
            $created_at = date('Y-m-d H:i:s');
            // 添加新的資料
            $products = addslashes(json_encode($products)); // \' \"
            $sql = "INSERT INTO {$prefix}cart(products, price, quantity, uid, created_at)
                    VALUES('$products', '$price', '$quantity', '$uid', '$created_at')";
            execute($sql);
        }
        header('location: cart.php');

        // json [{2: {quantity: 4, product: {id: , price: }}}, {}]
    } else {
        // 如果沒有登錄,將購物車資料存入到 session
        $cart = getSession('cart', 'shop');
        if (!empty($cart)) {
            // 更新session
            // 判斷當前購物車當中是否擁有當前商品
            if (array_key_exists($product_id, $cart['products'])) {
                // 如果有,更新當前商品的數量,購物車總價,總數量
                $cart['products'][$product_id]['quantity'] += $quantity;
            } else {
                // 如果沒有,新加入一個商品資訊到購物車當中,更新購物車總價,總數量
                $cart['products'][$product_id] = ['quantity' => $quantity, 'product' => $product];
            }
            $cart['price'] += $quantity * $product['price'];
            $cart['quantity'] += $quantity;
            setSession('cart', $cart, 'shop');
        } else {
            // 寫入session
            $products = [
                $product_id => [
                    'quantity' => $quantity,
                    'product' => $product
                ]
            ];
            $cart_data = [
                'products' => $products,
                'price' => $quantity * $product['price'],
                'quantity' => $quantity
            ];
            setSession('cart', $cart_data, 'shop');
        }
        header('location: index.php');
    }

洗掉購物車商品cart_del.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 15:06
     */
    require '../tools.func.php';
    require '../db.func.php';
    require 'auth.php';

    $product_id = intval($_GET['product_id']);
    if (empty($product_id)) {
        header('location: cart.php');
        exit;
    }
    // 0. auth
    // 1. 查詢當前用戶的購物車
    $prefix = getDBPrefix();
    $uid = getSession('id', 'shop');
    $sql = "SELECT id,price,quantity,products
            FROM {$prefix}cart WHERE uid = '$uid'";
    $cart = queryOne($sql);
    // 2. 將購物車當中對應id的商品洗掉,更新總價格,總數量
    $cart['products'] = json_decode($cart['products'], true);
    $cart['price'] -= $cart['products'][$product_id]['quantity'] * $cart['products'][$product_id]['product']['price'];
    $cart['quantity'] -= $cart['products'][$product_id]['quantity'];
    unset($cart['products'][$product_id]);
    $cart['products'] = addslashes(json_encode($cart['products']));
    $sql = "UPDATE {$prefix}cart 
            SET price = '{$cart['price']}',
            quantity = '{$cart['quantity']}',
            products = '{$cart['products']}'
            WHERE uid = '$uid'";
    if (execute($sql)) {
        setInfo('洗掉成功');
    } else {
        setInfo('洗掉失敗');
    }
    header('location: cart.php');

模擬下單頁面checkout.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 15:34
     */
    require '../tools.func.php';
    require '../db.func.php';
    require 'auth.php';
    // 1. 獲取當前用戶的購物車資料
    $uid = getSession('id', 'shop');
    $prefix = getDBPrefix();
    $sql = "SELECT id, price, quantity, products, uid
            FROM {$prefix}cart WHERE uid = '$uid'";
    $cart = queryOne($sql);
    if (empty($cart)) {
        header('location: cart.php');
        exit;
    }
    // 2. 將購物車資料寫入到訂單表當中 (id, price, quantity, products, created_at, uid)
    $price = $cart['price'];
    $quantity = $cart['quantity'];
    $products = $cart['products'];
    $created_at = date('Y-m-d H:i:s');
    $sql = "INSERT INTO {$prefix}order(price, quantity, products, created_at, uid)
            VALUES('$price', '$quantity', '$products', '$created_at', '$uid')";

    if (execute($sql)) {
        setInfo('下單成功');
    } else {
        setInfo('下單失敗');
    }

    header('location: order_status.php');

下單成功頁面order_status.php

    require 'header.php';
    ?>
    <div class="cart-main-area bg__white">
        <div class="container">
            <div class="row">
                <div class="col-md-12 col-sm-12 col-xs-12" style="text-align: center;padding: 50px;">
                    <h1 style="color: green;"><?php if (hasInfo()) echo getInfo(); ?></h1>
                </div>
            </div>
        </div>
    </div>

    <div class="only-banner ptb--10 bg__white">
    </div>
    <?php
    require 'footer.php';
    ?>

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86753.html

標籤:PHP

上一篇:電子商城專案開發(后臺功能模塊開發)

下一篇:面向物件

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more