效果图:
代码逻辑
新建表单文件 index.php
<?php
/*
* 数据库连接
* */
$mysqlconn = mysqli_connect("localhost", "root", "root"); // 连接数据库
if ($mysqlconn->error) {
die("数据库连接失败");
}
$mysqlconn->select_db("laravel-george"); // 选择数据库
$mysqlconn->query("set names gb2312"); // 设置编码
/*
* 获取页面切换的参数和数据库总行数
*
* */
$page = isset($_GET['page']) ? $_GET['page'] : 1; // 当前页
$pagesize = 5; // 每页显示的条数
$sql = "select count(*) from users";
$result = $mysqlconn->query($sql);
$maxrows = $result->fetch_row();
$maxrows = $maxrows[0];
$maxpage = ceil($maxrows / $pagesize); // 获取总页数
if ($page > $maxpage) { //判断是否超出最后一页
$page = $maxpage;
}
if ($page < 1) { // 判断是否小于第一页
$page = 1;
}
$offset = ($page - 1) * $pagesize; // 计算偏移量
/*
* 获取数据库中的数据循环显示
* */
$sql = "select * from users limit {$offset},$pagesize"; // 查询数据
$result = $mysqlconn->query($sql);
echo "<form action='test.php' method='post'>";
echo "用户信息表";
echo "<table border='1' >";
echo "<td>id</td>";
echo "<td>用户名</td>";
echo "<td>邮箱地址</td>";
echo "<td>操作</td>";
while ($rows = mysqli_fetch_assoc($result)) { // 循环遍历显示数据
echo "<tr>";
echo "<td>{$rows['id']}</td>";
echo "<td>{$rows['user']}</td>";
echo "<td>{$rows['email']}</td>";
echo "<td><input type='checkbox' name='member_id[]' value={$rows['id']}></td>";
// 使用函数转换日期
echo "</tr>";
}
echo "<td><input type='submit' value='删除' ></td>";
echo "</table>";
echo "</form>";
echo "<br>";
/*
* 分页链接
* */
echo "当前{$page}/{$maxpage}页 共计{$maxrows}条信息"; // 页码信息
echo "<br>";
echo "<a href='index.php?page=1'>首页</a> "; // 跳转到首页
echo "<a href='index.php?page=" . ($page - 1) . "'>上一页</a> "; // 跳转到上一页
echo "<a href='index.php?page=" . ($page + 1) . "'>下一页</a> "; // 跳转到下一页
echo "<a href='index.php?page=" . $maxpage . "'>最后一页</a> "; // 跳转到尾页
增加数据删除文件 deldata.php
<?php
if (!empty($_POST['member_id'])) { // 提交的信息不能为空
$arr = $_POST['member_id']; // 获取删除id的数组
$str_key=""; // 删除提示信息字符串
foreach($arr as $key=>$value){ // 对数组进行循环遍历
$sql="delete from db_members where member_id =".$value;
编写删除的SQL语句
mysql_query($sql); // 执行并删除
$str_key.=$value.","; // 拼接删除提示信息字符串
}
$new_str=substr($str_key,0,strlen($str_key)-1); // 截取字符串
echo"<script>alert('删除编号为".$new_str."的信息成功!');location='user.php'
</script>";
}