电子商城项目开发(后台功能模块开发)

后台登陆页login.php

    <?php
    //1.连接数据库 (创建一个数据库,创建数据表 test_admin)
    //id, adminuser, adminpass, created_at, login_at, login_ip
    require ‘../db.func.php‘;
    require ‘../tools.func.php‘;
    // POST提交
    if (!empty($_POST[‘adminuser‘])) {
      //2.查询用户名和密码是否正确 adminuser adminpass
        $prefix = getDBPrefix();
        $adminuser = htmlentities($_POST[‘adminuser‘]);
        $adminpass = md5(htmlentities($_POST[‘adminpass‘]));
        $sql = "SELECT id, adminuser FROM {$prefix}admin
                        WHERE adminuser = ‘$adminuser‘
                        AND adminpass = ‘$adminpass‘";

        $res = queryOne($sql);
        if ($res) {
        //3.写入session
            setSession(‘admin‘,
                [‘adminuser‘ => $adminuser, ‘id‘ => $res[‘id‘]]
            );
            $login_at = date(‘Y-m-d H:i:s‘);
            $ip = $_SERVER[‘REMOTE_ADDR‘] == ‘::1‘ ? ‘127.0.0.1‘ : $_SERVER[‘REMOTE_ADDR‘];
            $login_ip = ip2long($ip);
            $sql = "UPDATE {$prefix}admin
                            SET login_at = ‘$login_at‘, login_ip = ‘$login_ip‘
                            WHERE id = ‘{$res[‘id‘]}‘";
            execute($sql);
        //4.跳转到index.php
            header(‘location: index.php‘);
        } else {
            setInfo(‘用户名或者密码错误‘);
        }
    }

    ?>
    <!doctype html>
    <html>

    <head>
      <title>商城</title>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
      <!--     Fonts and icons     -->
      <link rel="stylesheet" type="text/css" href="assets/css/googlefonts.css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons" />
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
      <!-- Material Kit CSS -->
      <link href="assets/css/material-dashboard.css?v=2.1.1" rel="stylesheet" />
    </head>

    <body>
      <div class="wrapper ">
        <div>
          <div>
            <div class="container" style="width: 50%;margin-top: 250px;">
              <div class="row">
                <div class="col-md-12">
                  <div class="col-md-12">
                    <div class="card">

                      <div class="card-header card-header-primary">
                        <h4 class="card-title">登录</h4>
                        <p class="card-category">以管理员身份登录后台</p>
                      </div>
                      <div class="card-body">
                          <p><?php if (hasInfo()) echo getInfo(); ?></p>
                        <form action="login.php" method="post">
                          <div class="row">
                            <div class="col-md-12">
                              <div class="form-group">
                                <label class="bmd-label-floating">用户名</label>
                                <input type="text" name="adminuser" class="form-control">
                              </div>
                            </div>
                          </div>
                          <div class="row">
                            <div class="col-md-12">
                              <div class="form-group">
                                <label class="bmd-label-floating">密码</label>
                                <input type="password" name="adminpass" class="form-control">
                              </div>
                            </div>
                          </div>
                          <button type="submit" class="btn btn-primary pull-right">登录</button>
                          <div class="clearfix"></div>
                        </form>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <script src="assets/js/core/jquery.min.js"></script>
      <script src="assets/js/core/popper.min.js"></script>
      <script src="assets/js/core/bootstrap-material-design.min.js"></script>
    </body>

    </html>

数据库结构shop.sql

    /*
     Navicat Premium Data Transfer

     Source Server         : 127.0.0.1
     Source Server Type    : MySQL
     Source Server Version : 80012
     Source Host           : localhost:3306
     Source Schema         : shop

     Target Server Type    : MySQL
     Target Server Version : 80012
     File Encoding         : 65001

     Date: 26/01/2019 10:13:57
    */

    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;

    -- ----------------------------
    -- Table structure for test_admin
    -- ----------------------------
    DROP TABLE IF EXISTS `test_admin`;
    CREATE TABLE `test_admin` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `adminuser` varchar(50) NOT NULL DEFAULT ‘‘,
      `adminpass` char(32) NOT NULL DEFAULT ‘‘,
      `created_at` varchar(255) NOT NULL DEFAULT ‘‘,
      `login_at` varchar(255) NOT NULL DEFAULT ‘‘ ,
      `login_ip` bigint(20) NOT NULL DEFAULT ‘0‘,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_admin
    -- ----------------------------
    BEGIN;
    INSERT INTO `test_admin` VALUES (1, ‘admin‘, ‘0192023a7bbd73250516f069df18b500‘, ‘2019-01-23 20:21:03‘, ‘2019-01-24 12:56:48‘, 2130706433);
    COMMIT;

    -- ----------------------------
    -- Table structure for test_cart
    -- ----------------------------
    DROP TABLE IF EXISTS `test_cart`;
    CREATE TABLE `test_cart` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `price` decimal(10,2) unsigned NOT NULL DEFAULT ‘0.00‘,
      `quantity` int(10) unsigned NOT NULL DEFAULT ‘0‘,
      `products` text,
      `uid` int(10) unsigned NOT NULL DEFAULT ‘0‘,
      `created_at` varchar(255) NOT NULL DEFAULT ‘‘ ,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_cart
    -- ----------------------------
    BEGIN;
    INSERT INTO `test_cart` VALUES (2, 21700.00, 3, ‘{\"3\":{\"quantity\":2,\"product\":{\"id\":\"3\",\"name\":\"Macbook Pro\",\"price\":\"8800.00\",\"code\":\"88888888\",\"description\":\"Macbook Pro\"}},\"4\":{\"quantity\":1,\"product\":{\"id\":\"4\",\"name\":\"\\u534e\\u4e3a\\u624b\\u673a\",\"price\":\"4100.00\",\"code\":\"929868123123123\",\"description\":\"\\u5546\\u54c1\\u63cf\\u8ff0\\uff1a\\r\\n\\r\\n\\u8fd9\\u662f\\u534e\\u4e3a\\u624b\\u673a\"}}}‘, 5, ‘2019-01-24 10:53:24‘);
    COMMIT;

    -- ----------------------------
    -- Table structure for test_order
    -- ----------------------------
    DROP TABLE IF EXISTS `test_order`;
    CREATE TABLE `test_order` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `price` decimal(10,2) unsigned NOT NULL DEFAULT ‘0.00‘,
      `quantity` int(10) unsigned NOT NULL DEFAULT ‘0‘,
      `products` text,
      `uid` int(10) unsigned NOT NULL DEFAULT ‘0‘,
      `created_at` varchar(255) NOT NULL DEFAULT ‘‘ ,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_order
    -- ----------------------------
    BEGIN;
    INSERT INTO `test_order` VALUES (1, 17600.00, 2, ‘{\"3\":{\"quantity\":2,\"product\":{\"id\":\"3\",\"name\":\"Macbook Pro\",\"price\":\"8800.00\",\"code\":\"88888888\",\"description\":\"Macbook Pro\"}}}‘, 5, ‘2019-01-24 12:46:33‘);
    COMMIT;

    -- ----------------------------
    -- Table structure for test_product
    -- ----------------------------
    DROP TABLE IF EXISTS `test_product`;
    CREATE TABLE `test_product` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(200) NOT NULL DEFAULT ‘‘,
      `code` varchar(100) NOT NULL DEFAULT ‘‘,
      `description` text,
      `stock` int(10) unsigned NOT NULL DEFAULT ‘0‘,
      `price` decimal(10,2) unsigned NOT NULL DEFAULT ‘0.00‘,
     `created_at` varchar(255) NOT NULL DEFAULT ‘‘ ,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_product
    -- ----------------------------
    BEGIN;
    INSERT INTO `test_product` VALUES (3, ‘Macbook Pro‘, ‘88888888‘, ‘Macbook Pro‘, 99, 8800.00, ‘2019-01-24 00:19:28‘);
    INSERT INTO `test_product` VALUES (4, ‘华为手机‘, ‘929868123123123‘, ‘商品描述:\r\n\r\n这是华为手机‘, 99, 4100.00, ‘2019-01-24 00:31:28‘);
    COMMIT;

    -- ----------------------------
    -- Table structure for test_user
    -- ----------------------------
    DROP TABLE IF EXISTS `test_user`;
    CREATE TABLE `test_user` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `username` varchar(100) NOT NULL DEFAULT ‘‘,
      `password` char(32) NOT NULL DEFAULT ‘‘,
      `name` varchar(100) NOT NULL DEFAULT ‘‘,
      `age` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘,
      `email` varchar(100) NOT NULL DEFAULT ‘‘,
      `phone` varchar(20) NOT NULL DEFAULT ‘‘,
     `created_at` varchar(255) NOT NULL DEFAULT ‘‘ ,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

    -- ----------------------------
    -- Records of test_user
    -- ----------------------------
    BEGIN;
    INSERT INTO `test_user` VALUES (3, ‘zhangsan‘, ‘4297f44b13955235245b2497399d7a93‘, ‘张三‘, 28, ‘[email protected]‘, ‘13200000000‘, ‘2019-01-23 23:54:34‘);
    INSERT INTO `test_user` VALUES (4, ‘wangwu‘, ‘4297f44b13955235245b2497399d7a93‘, ‘‘, 0, ‘[email protected]‘, ‘‘, ‘2019-01-24 09:21:45‘);
    INSERT INTO `test_user` VALUES (5, ‘zhaoliu‘, ‘4297f44b13955235245b2497399d7a93‘, ‘‘, 0, ‘[email protected]‘, ‘‘, ‘2019-01-24 09:35:05‘);
    COMMIT;

    SET FOREIGN_KEY_CHECKS = 1;

配置数据库文件config.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/23
     * Time: 20:22
     */
    date_default_timezone_set(‘PRC‘);
    return [
        ‘DB_HOST‘ => ‘127.0.0.1‘,
        ‘DB_PORT‘ => ‘3306‘,
        ‘DB_USER‘ => ‘root‘,
        ‘DB_PASS‘ => ‘123456‘,
        ‘DB_NAME‘ => ‘test_shop‘,
        ‘DB_PREFIX‘ => ‘test_‘,
        ‘DB_CHARSET‘ => ‘utf8‘,
    ];

操作数据库函数db.func.php

    <?php

    function connect()
    {
        $config = require dirname(__FILE__) . ‘/config.php‘;
        $mysqli = @mysqli_connect(
            $config[‘DB_HOST‘] . ‘:‘ . $config[‘DB_PORT‘],
            $config[‘DB_USER‘],
            $config[‘DB_PASS‘],
            $config[‘DB_NAME‘]
        ) or die(‘Connect Error: ‘ . mysqli_connect_errno() . ‘-‘ . mysqli_connect_error());
        mysqli_set_charset($mysqli, $config[‘DB_CHARSET‘]);
        return $mysqli;
    }

    function queryOne($sql)
    {
        $mysqli = connect();
        $result = mysqli_query($mysqli, $sql);
        $data = [];
        if ($result && mysqli_num_rows($result) > 0) {
            $data = mysqli_fetch_assoc($result);
        }
        return $data;
    }

    function query($sql)
    {
        $mysqli = connect();
        $result = mysqli_query($mysqli, $sql);
        $data = [];
        if ($result && mysqli_num_rows($result) > 0) {
            while ($res = mysqli_fetch_assoc($result)) {
                $data[] = $res;
            }
        }
        return $data;
    }

    function getDBPrefix()
    {
        $config = require dirname(__FILE__) . ‘/config.php‘;
        return $config[‘DB_PREFIX‘];
    }

    function execute($sql)
    {
        $mysqli = connect();
        mysqli_query($mysqli, $sql);
        return mysqli_affected_rows($mysqli) > 0;
    }

公共函数文件tools.func.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/23
     * Time: 20:31
     */

    function setSession($key, $data, $prefix = ‘‘)
    {
        session_id() || @session_start();
        if (!empty($prefix)) {
            $_SESSION[$prefix][$key] = $data;
        } else {
            $_SESSION[$key] = $data;
        }
    }

    function getSession($key, $prefix = ‘‘)
    {
        session_id() || @session_start();
        if (!empty($prefix)) {
            return isset($_SESSION[$prefix][$key]) ? $_SESSION[$prefix][$key] : [];
        } else {
            return isset($_SESSION[$key]) ? $_SESSION[$key] : [];
        }
    }

    function deleteSession($key, $prefix = ‘‘)
    {
        session_id() || @session_start();
        if (!empty($prefix)) {
            $_SESSION[$prefix][$key] = null;
        } else {
            $_SESSION[$key] = null;
        }
    }

    function setInfo($info)
    {
        setSession(‘info‘, $info, ‘system‘);
    }

    function getInfo()
    {
       $info = getSession(‘info‘, ‘system‘);
       deleteSession(‘info‘, ‘system‘);
       return $info;
    }

    function hasInfo()
    {
       return !empty(getSession(‘info‘, ‘system‘));
    }

判断是否有登陆权限auth.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/23
     * Time: 22:07
     */

    if (empty(getSession(‘adminuser‘, ‘admin‘))) {
        header(‘location: login.php‘);
        exit;
    }

登陆成功后进入后台首页index.php

    <?php
    require ‘../db.func.php‘;
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    //1.查询数据库 test_admin
    //2.写sql语句
    $prefix = getDBPrefix();
    $sql = "SELECT id,adminuser,created_at,login_at,login_ip
                    FROM {$prefix}admin ORDER BY created_at DESC";
    $data = query($sql);
    //3.遍历数据

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <h4 class="card-title ">所有管理员</h4>
                    <p class="card-category"> 控制台所有管理员列表</p>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class=" text-primary">
                            <th>
                                ID
                            </th>
                            <th>
                                用户名
                            </th>
                            <th>
                                创建时间
                            </th>
                            <th>
                                最后登录时间
                            </th>
                            <th>
                                最后登录IP
                            </th>
                            </thead>
                            <tbody>
                            <?php foreach ($data as $admin): ?>
                            <tr>
                                <td>
                                    <?php echo $admin[‘id‘]; ?>
                                </td>
                                <td>
                    <?php echo $admin[‘adminuser‘]; ?>
                                </td>
                                <td>
                    <?php echo $admin[‘created_at‘]; ?>
                                </td>
                                <td>
                    <?php echo $admin[‘login_at‘]; ?>
                                </td>
                                <td>
                    <?php echo long2ip($admin[‘login_ip‘]); ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    require ‘footer.php‘;
    ?>

header.php

    <?php
    $script = basename($_SERVER[‘SCRIPT_FILENAME‘]);
    // 控制台 index.php admin_edit.php
    // 用户管理 users.php user_add.php user_edit.php
    // 商品管理 products.php product_add.php product_edit.php
    ?>
    <!doctype html>
    <html>

    <head>
        <title>商城</title>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <!--     Fonts and icons     -->
        <link rel="stylesheet" type="text/css"
              href="assets/css/googlefonts.css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css">
        <!-- Material Kit CSS -->
        <link href="assets/css/material-dashboard.css?v=2.1.1" rel="stylesheet"/>
    </head>

    <body>
    <div class="wrapper ">
        <div class="sidebar" data-color="purple" data-background-color="white">
            <div class="logo">
                <a href="index.php" class="simple-text logo-normal">
                    商城
                </a>
            </div>
            <div class="sidebar-wrapper">
                <ul class="nav">
                    <li class="nav-item <?php echo substr($script, 0, 5) == ‘index‘ || substr($script, 0, 5) == ‘admin‘ ? ‘active‘ : ‘‘; ?>">
                        <a class="nav-link" href="index.php">
                            <i class="material-icons">dashboard</i>
                            <p>控制台</p>
                        </a>
                    </li>
                    <li class="nav-item <?php echo substr($script, 0, 4) == ‘user‘ ? ‘active‘ : ‘‘; ?>">
                        <a class="nav-link" href="users.php">
                            <i class="material-icons">person</i>
                            <p>用户管理</p>
                        </a>
                    </li>
                    <li class="nav-item <?php echo substr($script, 0, 7) == ‘product‘ ? ‘active‘ : ‘‘; ?>">
                        <a class="nav-link" href="products.php">
                            <i class="material-icons">library_books</i>
                            <p>商品管理</p>
                        </a>
                    </li>
                    <li class="nav-item <?php echo substr($script, 0, 4) == ‘cart‘ ? ‘active‘ : ‘‘; ?>">
                        <a class="nav-link" href="carts.php">
                            <i class="material-icons">shopping_cart</i>
                            <p>购物车管理</p>
                        </a>
                    </li>
                    <li class="nav-item <?php echo substr($script, 0, 5) == ‘order‘ ? ‘active‘ : ‘‘; ?>">
                        <a class="nav-link" href="orders.php">
                            <i class="material-icons">list</i>
                            <p>订单管理</p>
                        </a>
                    </li>
                    <!-- your sidebar here -->
                </ul>
            </div>
        </div>
        <div class="main-panel">
            <!-- Navbar -->
            <nav class="navbar navbar-expand-lg navbar-transparent navbar-absolute fixed-top ">
                <div class="container-fluid">
                    <div class="navbar-wrapper">
                        <a class="navbar-brand" href="index.php">控制台</a>
                    </div>
                    <div class="collapse navbar-collapse justify-content-end">
                        <ul class="navbar-nav">
                            <li class="nav-item dropdown">
                                <a class="nav-link" href="#" id="navbarDropdownProfile" data-toggle="dropdown" aria-haspopup="true"
                                   aria-expanded="false">
                                    <i class="material-icons">person</i>
                                    <p class="d-lg-none d-md-block">
                                        管理员
                                    </p>
                                </a>
                                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownProfile">
                                    <a class="dropdown-item" href="admin_edit.php">编辑</a>
                                    <div class="dropdown-divider"></div>
                                    <a class="dropdown-item" href="logout.php">退出</a>
                                </div>
                            </li>
                            <!-- your navbar here -->
                        </ul>
                    </div>
                </div>
            </nav>
            <!-- End Navbar -->
            <div class="content">
                <div class="container-fluid">

footer.php

    </div>
    </div>
    </div>
    </div>
    <script src="assets/js/core/jquery.min.js"></script>
    <script src="assets/js/core/popper.min.js"></script>
    <script src="assets/js/core/bootstrap-material-design.min.js"></script>
    </body>

    </html>

管理员账号密码修改admin_edit.php

    <?php
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    require ‘../db.func.php‘;
    $current_user = getSession(‘admin‘);

    //1.判断是否为post提交
    if (!empty($_POST[‘adminpass‘])) {
      //2.验证新密码和确认密码是否一致
        $adminpass = md5(htmlentities($_POST[‘adminpass‘]));
        $newpass = htmlentities($_POST[‘newpass‘]);
        $confirmpass = htmlentities($_POST[‘confirmpass‘]);
        if ($newpass != $confirmpass) {
            setInfo(‘两次密码输入不一致‘);
        } else {
        //3.验证旧密码是否正确 (查询数据库 用id,adminpass)
        $prefix = getDBPrefix();
        $sql = "SELECT id FROM {$prefix}admin
                    WHERE id = ‘{$current_user[‘id‘]}‘
                    AND adminpass = ‘$adminpass‘
                    ";
        $res = queryOne($sql);
        //4.更新数据表 imooc_admin adminpass
        if ($res) {
          $pass = md5($newpass);
          $sql = "UPDATE {$prefix}admin
                    SET adminpass = ‘$pass‘
                    WHERE id = ‘{$current_user[‘id‘]}‘";
          if (execute($sql)) {
              setInfo(‘修改密码成功‘);
          } else {
              setInfo(‘修改密码失败‘);
          }
        } else {
            setInfo(‘旧密码不正确!‘);
        }
        }

        //5.显示结果到页面
    }

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <h4 class="card-title">修改密码</h4>
                    <p class="card-category">修改当前管理员密码</p>
                </div>
                <div class="card-body">
                    <?php if (hasInfo()) echo getInfo(); ?>
                    <form action="admin_edit.php" method="post">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">用户名</label>
                                    <input type="text" disabled name="adminuser" value="<?php echo $current_user[‘adminuser‘]; ?>" class="form-control">
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">旧密码</label>
                                    <input type="password" name="adminpass" class="form-control">
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">新密码</label>
                                    <input type="password" name="newpass" class="form-control">
                                </div>
                            </div>

                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">确认密码</label>
                                    <input type="password" name="confirmpass" class="form-control">
                                </div>
                            </div>
                        </div>

                        <button type="submit" class="btn btn-primary pull-right">修改</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>

    </div>
    <?php
    require ‘footer.php‘;
    ?>

管理员后台登出logout.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/23
     * Time: 22:06
     */

    // 1. 删除当前登录用户的session
    require ‘../tools.func.php‘;

    deleteSession(‘admin‘);

    header(‘location: login.php‘);

用户列表显示users.php

    <?php
    require ‘../db.func.php‘;
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    // 1. 写sql查询
    $prefix = getDBPrefix();
    $sql = "SELECT id, username, age, name, email, phone, created_at
                    FROM {$prefix}user ORDER BY created_at DESC";
    // 2. 执行查询
    $res = query($sql);
    // 3. 遍历结果

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <div class="row">
                        <div class="col-10">
                            <h4 class="card-title ">所有用户</h4>
                            <p class="card-category"> 用户列表</p>
                        </div>
                        <div class="col-2">
                            <a href="user_add.php" class="btn btn-round btn-info" style="margin-left: 20px;">添加用户</a>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <p><?php if (hasInfo()) echo getInfo(); ?></p>
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class=" text-primary">
                            <th>
                                ID
                            </th>
                            <th>
                                用户名
                            </th>
                            <th>
                                姓名
                            </th>
                            <th>
                                年龄
                            </th>
                            <th>
                                邮箱
                            </th>
                            <th>
                                联系电话
                            </th>
                            <th>
                                注册时间
                            </th>
                            <th>
                                操作
                            </th>
                            </thead>
                            <tbody>
                            <?php foreach ($res as $user): ?>
                            <tr>
                                <td>
                                    <?php echo $user[‘id‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘username‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘name‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘age‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘email‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘phone‘]; ?>
                                </td>
                                <td>
                    <?php echo $user[‘created_at‘]; ?>
                                </td>
                                <td>
                                    <a href="user_edit.php?id=<?php echo $user[‘id‘]; ?>">编辑</a>
                                    |
                                    <a href="user_del.php?id=<?php echo $user[‘id‘]; ?>">删除</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    require ‘footer.php‘;
    ?>

添加用户user_add.php

    <?php
    require ‘../tools.func.php‘;
    require ‘../db.func.php‘;
    require ‘auth.php‘;
    if (!empty($_POST[‘username‘])) {
        // 1. 接收post数据
        $username = htmlentities($_POST[‘username‘]);
        $password = htmlentities($_POST[‘password‘]);
        $confirmpass = htmlentities($_POST[‘confirmpass‘]);
        $name = htmlentities($_POST[‘name‘]);
        $age = htmlentities($_POST[‘age‘]);
        $email = htmlentities($_POST[‘email‘]);
        $phone = htmlentities($_POST[‘phone‘]);
        $created_at = date(‘Y-m-d H:i:s‘);
        $prefix = getDBPrefix();
        // 2. 验证密码输入是否一致
        if ($password != $confirmpass) {
            setInfo(‘两次密码输入不一致‘);
        } else {
            $password = md5($password);
        // 3. 写sql语句
            $sql = "INSERT INTO {$prefix}user(username, password, age, name, email, phone, created_at)
                            VALUES(‘$username‘, ‘$password‘, ‘$age‘, ‘$name‘, ‘$email‘, ‘$phone‘, ‘$created_at‘)";
        // 4. 执行添加,如果成功,显示成功信息
            if (execute($sql)) {
                setInfo(‘添加成功‘);
            } else {
          setInfo(‘添加失败‘);
            }
        }

    }

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <h4 class="card-title">添加用户</h4>
                    <p class="card-category">添加一个用户</p>
                </div>
                <div class="card-body">
                    <?php if (hasInfo()) echo getInfo(); ?>
                    <form action="user_add.php" method="post">
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="bmd-label-floating">用户名</label>
                                    <input type="text" name="username" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="bmd-label-floating">密码</label>
                                    <input type="password" name="password" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label class="bmd-label-floating">确认密码</label>
                                    <input type="password" name="confirmpass" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">姓名</label>
                                    <input type="text" name="name" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">年龄</label>
                                    <input type="number" name="age" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">联系电话</label>
                                    <input type="text" name="phone" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">电子邮箱</label>
                                    <input type="email" name="email" class="form-control">
                                </div>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary pull-right">添加用户</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>

    </div>
    <?php
    require ‘footer.php‘;
    ?>

修改用户信息user_edit.php

    <?php
    require ‘../db.func.php‘;
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    // 1. 接收id
    $id = intval($_GET[‘id‘]);
    if (empty($id)) {
        header(‘location: users.php‘);
    }
    // 2. 根据id查询用户
    $prefix = getDBPrefix();
    $sql = "SELECT id,username,age,email,phone,name
                    FROM {$prefix}user WHERE id = ‘$id‘";
    $current_user = queryOne($sql);
    if (empty($current_user)) {
      header(‘location: users.php‘);
    }
    // 3. 将查询出的用户的数据放入到表单当中
    // 4. 判断是否为post提交
    if (!empty($_POST[‘name‘])) {
      // 5. 接收post数据
        $name = htmlentities($_POST[‘name‘]);
        $age = htmlentities($_POST[‘age‘]);
        $email = htmlentities($_POST[‘email‘]);
        $phone = htmlentities($_POST[‘phone‘]);
        // 6. 更新数据记录
        $sql = "UPDATE {$prefix}user
                        SET name = ‘$name‘, age = ‘$age‘, email = ‘$email‘, phone = ‘$phone‘
                        WHERE id = ‘$id‘";
        if (execute($sql)) {
        $current_user = array_merge($current_user, $_POST);
            setInfo(‘更新成功‘);
        } else {
            setInfo(‘更新失败‘);
        }
        // 7. 显示结果
    }

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <h4 class="card-title">修改用户</h4>
                    <p class="card-category">修改一个用户</p>
                </div>
                <div class="card-body">
                    <?php if (hasInfo()) echo getInfo(); ?>
                    <form action="user_edit.php?id=<?php echo $id; ?>" method="post">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">用户名</label>
                                    <input type="text" name="username" value="<?php echo $current_user[‘username‘]; ?>" disabled class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">姓名</label>
                                    <input type="text" name="name" value="<?php echo $current_user[‘name‘]; ?>" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">年龄</label>
                                    <input type="number" name="age" value="<?php echo $current_user[‘age‘]; ?>" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">联系电话</label>
                                    <input type="text" name="phone" value="<?php echo $current_user[‘phone‘]; ?>" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="bmd-label-floating">电子邮箱</label>
                                    <input type="email" name="email" value="<?php echo $current_user[‘email‘]; ?>" class="form-control">
                                </div>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary pull-right">更新信息</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>

    </div>
    <?php
    require ‘footer.php‘;
    ?>

删除用户user_del.php

    <?php
    /**
     * Created by PhpStorm.
     * Date: 2019/1/24
     * Time: 10:49
     */
    require ‘../db.func.php‘;
    require ‘../tools.func.php‘;
    require ‘auth.php‘;

    // 1. 接收id
    $id = intval($_GET[‘id‘]);
    // 2. 从数据库当中删除对应的数据
    $prefix = getDBPrefix();
    $sql = "DELETE FROM {$prefix}user WHERE id = ‘$id‘";
    if (execute($sql)) {
        setInfo(‘删除成功‘);
    } else {
        setInfo(‘删除失败‘);
    }
    // 3. 跳回到列表页
    header(‘location: users.php‘);

商品列表products.php

    <?php
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    require ‘../db.func.php‘;
    $prefix = getDBPrefix();
    $sql = "SELECT * FROM {$prefix}product ORDER BY created_at DESC ";
    $data = query($sql);
    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <div class="row">
                        <div class="col-10">
                            <h4 class="card-title ">所有商品</h4>
                            <p class="card-category"> 所有商品列表</p>
                        </div>
                        <div class="col-2">
                            <a href="product_add.php" class="btn btn-round btn-info" style="margin-left: 20px;">添加商品</a>
                        </div>
                    </div>

                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover" style="table-layout:fixed; ">
                            <thead class=" text-primary">
                            <th width="5%">
                                ID
                            </th>
                            <th>
                                商品编号
                            </th>
                            <th>
                                商品名称
                            </th>
                            <th>
                                商品描述
                            </th>
                            <th>
                                商品库存
                            </th>
                            <th>
                                商品单价
                            </th>
                            <th>
                                商品上架时间
                            </th>
                            <th>
                                编辑
                            </th>
                            </thead>
                            <tbody>
                <?php foreach ($data as $pro): ?>
                                <tr>
                                    <td>
                        <?php echo $pro[‘id‘]; ?>
                                    </td>
                                    <td>
                        <?php echo $pro[‘code‘]; ?>
                                    </td>
                                    <td>
                        <?php echo $pro[‘name‘]; ?>
                                    </td>
                                    <td>
                        <?php echo mb_substr($pro[‘description‘], 0, 8, ‘utf-8‘) . ‘...‘; ?>
                                    </td>
                                    <td>
                        <?php echo $pro[‘stock‘]; ?>
                                    </td>
                                    <td>
                        <?php echo $pro[‘price‘]; ?>
                                    </td>
                                    <td>
                        <?php echo $pro[‘created_at‘]; ?>
                                    </td>
                                    <td>
                                        <a href="#">编辑</a>
                                        |
                                        <a href="#">删除</a>
                                    </td>
                                </tr>
                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    require ‘footer.php‘;
    ?>

添加商品product_add.php

    <?php
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    require ‘../db.func.php‘;
    // 1. 判断是否为post提交
    if (!empty($_POST[‘name‘])) {
      // 2. 接收post数据
        $name = htmlentities($_POST[‘name‘]);
        $code = htmlentities($_POST[‘code‘]);
        $price = doubleval($_POST[‘price‘]);
        $stock = intval($_POST[‘stock‘]);
        $description = htmlentities($_POST[‘description‘]);
        $created_at = date(‘Y-m-d H:i:s‘);
        // 3. 写sql语句
        $prefix = getDBPrefix();
        $sql = "INSERT INTO {$prefix}product(name, code, price, stock, description, created_at)
                        VALUES(‘$name‘, ‘$code‘, ‘$price‘, ‘$stock‘, ‘$description‘, ‘$created_at‘)";
        // 4. 执行插入
        if (execute($sql)) {
            setInfo(‘添加成功‘);
        } else {
        setInfo(‘添加失败‘);
        }
        // 5. 显示结果
    }

    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <h4 class="card-title">添加商品</h4>
                    <p class="card-category">添加一个商品</p>
                </div>
                <div class="card-body">
                    <?php if (hasInfo()) echo getInfo(); ?>
                    <form action="product_add.php" method="post">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">商品名称</label>
                                    <input type="text" name="name" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">商品单价</label>
                                    <input type="number" name="price" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">商品库存</label>
                                    <input type="number" name="stock" class="form-control">
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="bmd-label-floating">商品编号</label>
                                    <input type="text" name="code" class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>商品描述</label>
                                    <div class="form-group bmd-form-group">
                                        <textarea name="description" class="form-control" rows="5"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary pull-right">添加商品</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>

    </div>
    <?php
    require ‘footer.php‘;
    ?>

购物车列表页carts.php

    <?php
    require ‘../tools.func.php‘;
    require ‘../db.func.php‘;
    require ‘auth.php‘;
    $prefix = getDBPrefix();
    $sql = "SELECT id, price, quantity, uid, created_at FROM {$prefix}cart ORDER BY created_at DESC";
    $back_cart_data = [];
    $cart = query($sql);
    foreach ($cart as $c) {
      $sql = "SELECT username FROM {$prefix}user WHERE id = ‘{$c[‘uid‘]}‘";
      $user = queryOne($sql);
      $c[‘username‘] = $user[‘username‘];
      $back_cart_data[] = $c;
    }
    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <div class="row">
                        <div class="col-12">
                            <h4 class="card-title ">所有购物车</h4>
                            <p class="card-category"> 所有购物车列表</p>
                        </div>
                    </div>

                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class=" text-primary">
                            <th>
                                ID
                            </th>
                            <th>
                                购物车用户
                            </th>
                            <th>
                                商品总量
                            </th>
                            <th>
                                购物车总价
                            </th>
                            <th>
                                添加时间
                            </th>
                            <th>
                                编辑
                            </th>
                            </thead>
                            <tbody>
                            <?php foreach ($back_cart_data as $cart): ?>
                            <tr>
                                <td>
                                    <?php echo $cart[‘id‘]; ?>
                                </td>
                                <td>
                    <?php echo $cart[‘username‘]; ?>
                                </td>
                                <td>
                    <?php echo $cart[‘quantity‘]; ?>
                                </td>
                                <td>
                    <?php echo $cart[‘price‘]; ?>
                                </td>
                                <td>
                    <?php echo $cart[‘created_at‘]; ?>
                                </td>
                                <td>
                                    <a href="">删除</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    require ‘footer.php‘;
    ?>

订单列表orders.php

    <?php
    require ‘../tools.func.php‘;
    require ‘auth.php‘;
    require ‘../db.func.php‘;
    $prefix = getDBPrefix();
    $sql = "SELECT id, uid, price, quantity, created_at
                    FROM {$prefix}order ORDER BY created_at DESC";
    $orders = query($sql);
    require ‘header.php‘;
    ?>
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header card-header-primary">
                    <div class="row">
                        <div class="col-12">
                            <h4 class="card-title ">所有订单</h4>
                            <p class="card-category"> 所有订单列表</p>
                        </div>
                    </div>

                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class=" text-primary">
                            <th>
                                ID
                            </th>
                            <th>
                                下单用户
                            </th>
                            <th>
                                订单价格
                            </th>
                            <th>
                                订单商品数量
                            </th>
                            <th>
                                下单时间
                            </th>
                            </thead>
                            <tbody>
                            <?php foreach($orders as $order): ?>
                            <tr>
                                <td>
                                    <?php echo $order[‘id‘]; ?>
                                </td>
                                <td>
                    <?php echo $order[‘uid‘]; ?>
                                </td>
                                <td>
                    <?php echo $order[‘price‘]; ?>
                                </td>
                                <td>
                      <?php echo $order[‘quantity‘]; ?>
                                </td>
                                <td>
                    <?php echo $order[‘created_at‘]; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    require ‘footer.php‘;
    ?>

原文地址:https://www.cnblogs.com/chenyingying0/p/12187008.html

时间: 2024-11-07 08:42:24

电子商城项目开发(后台功能模块开发)的相关文章

Drupal8开发教程:模块开发——创建新页面

之前我们已经通过<Drupal8开发教程:认识.info.yml文件>对模块的YAML文件有了了解,今天我们来看如何通过模块开发的方式添加一个新的页面. 在 Drupal 7 中,通过模块添加新页面非常简单,使用 hook_menu 定义路径及回调函数返回要显示的结果即可.但 Drupal 8 却要麻烦很多,虽然也是定义路径,然后关联用于显示内容的函数,但却要在两个文件中分开进行.而且 Controller 文件还有目录结构要求,这些变化都需要慢慢来适应了. 除了上文中定义的 hello_wo

iOS开发之功能模块--高仿Boss直聘的常用语的开发

首先上Boss直聘的功能界面截图,至于交互请读者现在Boss直聘去交互体验:     项目要高仿Boss直聘的IM常用语的交互功能,居然花费了我前后17个小时完成,这回自己测试了很多遍,代码质量很高,交互很流畅,仿真度也很高,重点综合性的用到了很多东西:runtime.代理传值.block传值.textView的系统通知监听.富文本.自定义封装.本地化增删改等等.其实这些都是基础,重点是功能的交互逻辑或者是业务逻辑的分析,然后正确实现,并经过尽可能多情况的自我测试通过,没有基础Bug,也没有任何

webApp开发-angular1+ 功能模块划分

js: 控制器命名app.js定义的带路由的控制器,子页面里单独声明的控制器 app_demoCtr page_demoCtr 服务命名.factory('common',function($state,$ionicHistory,$ionicScrollDelegate){return{init: function($scope){},}}) 公共函数使用(angular1):刷新(加载),返回,跳转,下拉服务类 发送短信验证码服务 调用接口服务 文件读取服务 图片缓存服务 服务+$ionic

php # 电子商城项目#

完成电子商务 项目的  前后端 商品模块的开发 其中要 重点掌握的 知识点 : OOP思想 ,MVC模式,数据库设计,复杂查询,无线分类,项目安全,iframe高级用法. OOP(Object Oriented Programming,面向对象编程 具有三大特征 1,封装: 隐藏内部实现的细节,给外部一个接口: 2,继承: 子类可以继承父类属性和方法(private): 3:多态:多种形态:组件的引用和类集会涉及到其它许多不同类型的组件,而且引用组件所产生的结果得依据实际调用的类型. OOP 的

js 模块开发之一(模块开发价值)

首先引用我们的今天的主角 ----<前端模块化开发的价值> 1,前端开发最常见的两个问题 ---命名冲突和文件依赖 2,对于命名冲突的基本解决办法就是学习其他语言的习惯,添加命名空间 var org = {}; org.CoolSite = {}; org.CoolSite.Utils = {}; org.CoolSite.Utils.each = function (arr) { // 实现代码 }; org.CoolSite.Utils.log = function (str) { //

iOS开发之功能模块--推送之坑问题解决

苹果开发证书中,有一个根证书,根证书可以绑定相关的开发者设备合作开发一个app项目,如果是多人团队一般根证书是由老大搞定和统一管理我们的设备,这时候老大他会用到他电脑的keychain来生成P12文件,然后并把根证书一并给我们用. 但是,开发苹果推送功能的人不一定是老大,比如老大交给我来弄这个推送证书并给app添加推送功能,而生成苹果的推送cer后缀的证书,就当然在我的电脑生成了,当然肯定要通过老大用的那个开发者账号登录苹果官网,并且要下载和app同一个bundle ID一致的推送证书: 下载好

asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发3-登录模块开发

进行本文之前需要在数据库用户表里面增加一条用户数据,直接手动添加即可,未安全考虑密码一定要使用Md5加密后的,这里提供666666的Md5密文为(c831b04de153469d),本文完成登录模块的开发工作,登录页面设计将采用Div+Css简单进行设计,最终效果图如下: Mvc一个页面模块至少要实现C层的逻辑代码跟,V层的页面代码,在浏览器访问地址时执行C层代码,在呈现页面,具体创建流程如下: 一.登录C层(Controllers)创建 1.创建Login控制器类 在解决方案管理器Contro

IOS开发之功能模块--给任意的UIView添加点击事件

前言:好久没写博客,今天来一波.我在实际项目开发中,会遇到这样功能需求:我已经搭好了iOS的UI界面,但是很多界面的子View用了UIView,然后这些UIView中用了UILabel和UIImageView,然后接着需求要这些View能够有点击事件,比如一点击就跳转到某个控制器.一开始懵逼了,难道我还要把他们全部改成UIButton,但是UIButton不好添加子View,难道我还要全部自定义一下UIButton,就算自定义好了,在项目已经打好的界面还是要一个个替换,多麻烦.所以就想到通过UI

iOS开发之功能模块--Apns推送中的的json格式介绍

在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 aps 字典.它使用字符串而不是字典作为 alert 的值,该负载同样包含了一个自定义的属性数组. { "aps" : { "alert" : "message" },//alert表示推送的消息文本 "parm" : [ &quo