C:\Users\xujunhao\Desktop\shop\backend\admin\index.php
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代码html
C:\Users\xujunhao\Desktop\shop\backend\admin\index.php
sql
<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>
复制代码
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 method="post">
<input type="text" name="username" class="form-control">
书写php逻辑代码函数
C:\Users\xujunhao\Desktop\shop\backend\admin\user_add.php
post
<?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('添加用户失败!');
}
}
?>
复制代码
省略代码...
<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>
省略代码...
复制代码
$_GET
获取...
C:\Users\xujunhao\Desktop\shop\backend\admin\users.php
ui
<td>
<a href="user_edit.php?id=<?php echo $user['id']?>">编辑</a>
|
<a href="user_del.php?id=<?php echo $user['id']?>">删除</a>
</td>
复制代码
C:\Users\xujunhao\Desktop\shop\backend\admin\user_edit.php
url
<?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("用户信息更新失败!");
}
}
?>
复制代码
$_GET
获取...
C:\Users\xujunhao\Desktop\shop\backend\admin\users.php
spa
<td>
<a href="user_edit.php?id=<?php echo $user['id']?>">编辑</a>
|
<a href="user_del.php?id=<?php echo $user['id']?>">删除</a>
</td>
复制代码
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');
复制代码