欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-07 19:18:39
后台登陆页login.php

后台登陆页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, '965794175@qq.com', '13200000000', '2019-01-23 23:54:34');
    insert into `test_user` values (4, 'wangwu', '4297f44b13955235245b2497399d7a93', '', 0, 'wangwu@test.com', '', '2019-01-24 09:21:45');
    insert into `test_user` values (5, 'zhaoliu', '4297f44b13955235245b2497399d7a93', '', 0, 'zhaoliu@test.com', '', '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';
    ?>