用户列表展示
- 掐头去尾
- 引入函数库
- 查询数据库, 获取信息
- 展示信息
C:\Users\xujunhao\Desktop\shop\backend\admin\index.php
<?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代码
C:\Users\xujunhao\Desktop\shop\backend\admin\index.php
<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
- 判断当前文件名, 根据文件名, 添加class
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页面
- 添加form的提交方式,
<form method="post">
- 添加input的name
<input type="text" name="username" class="form-control">
- 添加form的提交方式,
-
书写php逻辑代码
C:\Users\xujunhao\Desktop\shop\backend\admin\user_add.php
<?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.php
<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.php
<?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.php
<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');