欢迎光临!

 

 

接着之前那篇 “电子商城项目开发(后台功能模块开发)”

登陆页面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 = 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 = 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';
    ?>
复制代码
posted on 2020-02-15 20:05  花拉子米  阅读(443)  评论(0编辑  收藏  举报
版权所有