[PHP從小白到大牛]-027 PHP-商城項目(三)

用戶列表展現

  • 掐頭去尾
  • 引入函數庫
  • 查詢數據庫, 獲取信息
  • 展現信息

C:\Users\xujunhao\Desktop\shop\backend\admin\index.phpphp

<?php
session_id() ||session_start();
if(empty($_SESSION['admin'])){
  header('iocation:login.php');
}
require_once '../db.func.php';
require_once '../tools.func.php';
$prefix = getDBPrefix();
$sql = "select id,adminuser,created_at,login_at,login_ip from {$prefix}admin order by created_at desc";
$result = queryAll($sql);
include_once 'header.php';
?>
複製代碼

展現用戶信息的html代碼html

C:\Users\xujunhao\Desktop\shop\backend\admin\index.phpsql

<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 ($result as $value): ?>
    <tr>
      <td><?php echo $value['id']; ?></td>
      <td><?php echo $value['adminuser']; ?></td>
      <td><?php echo $value['created_at']; ?></td>
      <td><?php echo $value['login_at']; ?></td>
      <td><?php echo long2ip($value['login_ip']); ?></td>
    </tr>
    <?php endforeach;?>
  </tbody>
</table>
複製代碼

後臺用戶管理模塊-添加用戶操做

  • 側邊欄的激活狀態
    • 判斷當前文件名, 根據文件名, 添加class active

C:\Users\xujunhao\Desktop\shop\backend\admin\header.php數據庫

省略代碼...
<li class="nav-item <?php if(substr($current_file_name,0,5) == 'index' || substr($current_file_name,0,5) == 'admin') echo 'active'; ?>" >
    <a class="nav-link" href="index.php">
        <i class="material-icons">dashboard</i>
        <p>控制檯</p>
    </a>
</li>
<li class="nav-item <?php if(substr($current_file_name,0,4 ) == 'user') echo 'active'; ?>" >
    <a class="nav-link" href="users.php">
        <i class="material-icons">person</i>
        <p>用戶管理</p>
    </a>
</li>
<li class="nav-item <?php if(substr($current_file_name,0,7 ) == 'product') echo 'active'; ?>" >
    <a class="nav-link" href="products.php">
        <i class="material-icons">library_books</i>
        <p>商品管理</p>
    </a>
</li>
省略代碼...
複製代碼
  • 添加用戶頁面 ==> user_add.php, 注意掐頭去尾
  • 建立用戶表
CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
  `username` varchar(100) NOT NULL DEFAULT '' COMMENT '登陸名',
  `password` char(32) NOT NULL DEFAULT '' COMMENT '登陸密碼',
  `name` varchar(100) NOT NULL DEFAULT '' COMMENT '暱稱',
  `age` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '年齡',
  `email` varchar(100) NOT NULL DEFAULT '' COMMENT '郵箱',
  `phone` char(11) NOT NULL DEFAULT '' COMMENT '手機號',
  `created_at` datetime NOT NULL COMMENT '建立時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
複製代碼
  • 修改html頁面session

    • 添加form的提交方式, <form method="post">
    • 添加input的name <input type="text" name="username" class="form-control">
  • 書寫php邏輯代碼函數

C:\Users\xujunhao\Desktop\shop\backend\admin\user_add.phppost

<?php
// 引入文件
require_once "../db.func.php";
require_once "../tools.func.php";
// 獲去數據庫前綴
$prefix = getDBPrefix();
// 若是post提交...
if (!empty($_POST)) {
    // 書寫表單驗證規則
    $rules = [
        'username' => [
            'name' => '用戶名',
            'require' => true,
            'is_unique' => "select * from {$prefix}user where username = '" . $_POST['username'] . "'",
        ],
        'password' => [
            'name' => '用戶密碼',
            'require' => true,
        ],
        'confirm_password' => [
            'name' => '確認密碼',
            'require' => true,
            'is_equal' => 'password',
        ],
        'name' => [
            'name' => '用戶姓名',
            'require' => true,
        ],
        'age' => [
            'name' => '年齡',
            'require' => true,
            'type' => 'age',
        ],
        'phone' => [
            'name' => '手機號',
            'require' => true,
            'type' => 'phone',
            'is_unique' => "select * from {$prefix}user where phone = '" . $_POST['phone'] . "'",
        ],
        'email' => [
            'name' => '郵箱',
            'require' => true,
            'type' => 'email',
            'is_unique' => "select * from {$prefix}user where email = '" . $_POST['email'] . "'",
        ],
    ];

}
// 若是post提交, 且數據經過form表單驗證
if (!empty($_POST) && check_form($_POST, $rules)) {
    // 拼接sql語句, 寫入數據庫
    $username = $_POST['username'];
    $password = md5('yunhe_' . md5($_POST['password']));
    $name = $_POST['name'];
    $age = $_POST['age'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $created_at = date('Y-m-d H:i:s');
    $sql = "insert INTO `{$prefix}user`(`username`, `password`, `name`, `age`, `email`, `phone`, `created_at`) VALUES ('{$username}', '{$password}', '{$name}', {$age}, '{$email}', '{$phone}', '{$created_at}')";
    if (execute($sql)) {
        setInfo("成功添加用戶: {$username}", 'users.php');
        header('location:users.php');
    } else {
        setInfo('添加用戶失敗!');
    }
}
?>
複製代碼
  • 若是你須要保留填寫的信息, 那麼你須要獲取以前post提交的信息
省略代碼...
<div class="form-group">
  <label class="bmd-label-floating">用戶名</label>
  <input type="text" name="username" value="<?php if (isset($_POST['username'])) {echo $_POST['username'];}?>" class="form-control" />
</div>
省略代碼...
複製代碼

後臺用戶管理模塊-修改用戶

  • 添加頁面 user_edit.php
    • 去除公共部分, 掐頭去尾
  • 獲取用戶id
    • 在用戶列表頁面, 設置id
    • 經過url傳遞, 再經過$_GET獲取...

C:\Users\xujunhao\Desktop\shop\backend\admin\users.phpui

<td>
  <a href="user_edit.php?id=<?php echo $user['id']?>">編輯</a>
  |
  <a href="user_del.php?id=<?php echo $user['id']?>">刪除</a>
</td>
複製代碼
  • 書寫PHP代碼

C:\Users\xujunhao\Desktop\shop\backend\admin\user_edit.phpurl

<?php
// 引入文件
require_once '../db.func.php';
require_once '../tools.func.php';
// 獲取須要修改的用戶id
$id = $_GET['id'];
// 獲取數據表的前綴
$prefix = getDBPrefix();
// 根據id查詢用戶信息, 展現在頁面上
$sql = "select username,name,age,phone,email from {$prefix}user where id = $id";
$userInfo = queryOne($sql);
// 若是是post提交, 檢查表單信息, 是否符合規範
if (!empty($_POST)) {
    // 驗證規則
    $rules = [
        'name' => [
            'name' => '姓名',
            'require' => true,
        ],
        'age' => [
            'name' => '年齡',
            'require' => true,
            'type' => 'age',
        ],
        'phone' => [
            'name' => '手機號',
            'require' => true,
            'type' => 'phone',
            'is_unique' => "select id from {$prefix}user where phone = '{$_POST['phone']}' and not id = $id",
        ],
        'email' => [
            'name' => '郵箱',
            'require' => true,
            'type' => 'email',
            'is_unique' => "select id from {$prefix}user where email = '{$_POST['email']}' and not id = $id",
        ],
    ];

}
// 若是post提交, 而且表單驗證沒有問題
if (!empty($_POST) && check_form($_POST, $rules)) {
    $name = $_POST['name'];
    $age = $_POST['age'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    // 拼接sql語句進行更新...
    $sql = "UPDATE `{$prefix}user` SET `name` = '{$name}', `age` = {$age}, `email` = '{$email}', `phone` = '{$phone}' WHERE `id` = $id";
    // 執行sql語句
    if (execute($sql)) {
        // setInfo("用戶信息更新成功!");
        header('location:users.php');
    } else {
        setInfo("用戶信息更新失敗!");
    }
}
?>
複製代碼

刪除用戶

  • 獲取用戶id
    • 在用戶列表頁面, 設置id
    • 經過url傳遞, 再經過$_GET獲取...

C:\Users\xujunhao\Desktop\shop\backend\admin\users.phpspa

<td>
  <a href="user_edit.php?id=<?php echo $user['id']?>">編輯</a>
  |
  <a href="user_del.php?id=<?php echo $user['id']?>">刪除</a>
</td>
複製代碼
  • 從數據庫中, 刪除指定id的用戶

C:\Users\xujunhao\Desktop\shop\backend\admin\user_del.php

<?php
// 引入文件
require_once '../db.func.php';
require_once '../tools.func.php';
// 獲取表前綴
$prefix = getDBPrefix();
// 獲取要刪除的用戶id
$id = $_GET['id'];
// 拼接刪除用戶的sql語句
$sql = "delete from {$prefix}user where id = {$id}";
// 執行sql語句
if (execute($sql)) {
    setInfo("ID爲 {$id} 的用戶刪除成功!!!");
} else {
    setInfo("ID爲 {$id} 的用戶刪除失敗!");
}
// 跳轉到用戶列表頁
header('location:users.php');
複製代碼
相關文章
相關標籤/搜索