Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:极棒,推荐了
1.代码文档:
ID | 文件名 | 注释 |
---|---|---|
1 | connect.php |
连接数据库 |
2 | control.php |
跳转编辑页面,保存数据编辑及删除数据操作 |
3 | edit.php |
编辑页面 |
4 | list.php |
查询数据操作 |
5 | user_list.php |
用户列表页面 |
2.文件代码部分:
connect.php
<?php
//数据库参数
$param =[
'DB_HOST'=>'localhost',
'DB_TYPE'=>'mysql',
'DB_NAME'=>'my_user',
'DB_PASSWORD'=>'root123',
'DB_USER'=>'root',
'DB_CHARSET'=>'utf8',
'DB_PORT'=>'3306'
];
$dsn = "mysql:host=".$param['DB_HOST'].";dbname=".$param['DB_NAME'].";charset=".$param['DB_CHARSET'];
//连接数据
try{
//连接数据款
$pdo = new PDO($dsn,$param['DB_USER'],$param['DB_PASSWORD']);
} catch(PDOException $e){
//捕捉特定于数据库信息的PDOEXCEPTION 异常
echo $e->getMessage();
} catch(Throwable $e){
//捕捉拥有Throwable接口的错误或者其他异常
echo $e->getMessage();
}
control.php
<?php
require "connect.php";
$act = $_GET['act'];
$id = $_GET['id'];
switch($act){
case 'edit':
include "edit.php";
break;
case 'saveedit':
$username =$_POST['username'];
$phone =$_POST['phone'];
if(!empty($username) && !empty($phone)){
$sql = " UPDATE `mu_user` SET `username`=?,`phone`=? WHERE `id`=?";
$stm = $pdo->prepare($sql);
// $sql = 'UPDATE `mu_user` SET `username`=?, `phone`=? WHERE `id`=?';
// $stm = $pdo->prepare($sql);
$stm->bindParam(1,$username);
$stm->bindParam(2,$phone);
$stm->bindParam(3,$id);
$stm->execute();
// var_dump($stm->rowCount());
if($stm->rowCount()==1){
exit("
<script>
alert('保存成功');
location.href='./user_list.php';
</script>
");
}else{
exit("
<script>
alert('保存失败');
location.href='control.php?act=edit&id=".$id."';
</script>
");
}
}else{
exit( "<script>alert('参数不能为空');location.href='control.php?act=edit&id=".$id."';</script>");
}
break;
case 'del':
$sql = "DELETE FROM `mu_user` WHERE `id`=?";
$stm = $pdo->prepare($sql);
$stm->bindParam(1,$id);
$stm->execute();
if($stm->rowCount()==1){
exit("
<script>
alert('删除成功');
location.href='./user_list.php';
</script>
");
}
break;
}
edit.php
<?php
require "connect.php";
$id = $_GET['id'];
$sql = "SELECT * FROM `mu_user` WHERE id=?";
$stm = $pdo->prepare($sql);
$stm->bindParam(1,$id);
$stm->execute();
$user = $stm->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑页面</title>
</head>
<style>
h1{
text-align: center;
}
form{
border: 1px solid rosybrown;
background-color: sandybrown;
width: 300px;
margin: 0 auto;
}
form div{
margin-top: 15px;
margin-left: 10%;
margin-bottom: 10px;
}
/* form>input{
margin-top: 15px;
margin-left: 20%;
} */
</style>
<body>
<h1>用户编辑</h1>
<form action="./control.php?act=saveedit&id=<?=$id?>" method="POST">
<div>
<label for="username">姓名</label>
<input type="text" name="username" id="username" value="<?php echo $user['username']?>">
</div>
<div>
<label for="phone">电话</label>
<input type="text" name="phone" id="phone" value="<?php echo $user['phone']?>">
</div>
<div>
<div><input type="submit" value="保存"></div>
</form>
</body>
</html>
list.php
<?php
require "connect.php";
//每页几条
$limit =5;
//当前也是\
$page = $_GET['p'];
if(empty($page))$page = 1;
//每页起始位置
$start = ($page-1)*$limit;
$sql = "SELECT * FROM `mu_user` LIMIT ?,?";
$stm = $pdo->prepare($sql);
//绑定LIMIT占位符必须传入第三个参数PDO::PARAM_INT
$stm->bindParam(1,$start,PDO::PARAM_INT);
$stm->bindParam(2,$limit,PDO::PARAM_INT);
// $stm->bindParam(1,$limit);
$stm->execute();
// fetchAll — 返回一个包含结果集中所有行的数组
$result = $stm->fetchAll(PDO::FETCH_ASSOC);
$sql2 = "SELECT COUNT('id') as num FROM `mu_user` ";
//总条数
$num = $pdo->query($sql2)->fetch()['num'];
//总页数
$pages = ceil($num/$limit);
user_list.php
<?php
require "list.php";
//当前获取网页地址
$url = $_SERVER['PHP_SELF'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用户列表</title>
</head>
<style>
table{
text-align: center;
border:2px solid #222;
border-collapse: collapse;
margin: 0 auto;
}
tr:first-of-type{
background-color:thistle;
}
td{
padding: 10px;
border:1px solid #000;
}
p{
text-align: center;
}
a{
text-align: center;
border:1px solid #000;
text-decoration: none;
padding: 5px 10px;
margin: 10px 2px;
border-radius: 10%;
}
p>span{
font-size: 1.5rem;
margin: auto;
padding: 10px;
}
.cla{
background-color: lightskyblue;
color: lightgoldenrodyellow;
border: 1px solid lightskyblue;
}
</style>
<body>
<table>
<caption>用户列表</caption>
<tr>
<td>ID</td>
<td>名称</td>
<td>电话</td>
<td>操作</td>
</tr>
<?php
foreach( $result as $list){
?>
<tr>
<td><?=$list['id']?></td>
<td><?php echo $list['username'];?></td>
<td><?php echo $list['phone'];?></td>
<td>
<button onclick="location.href='control.php?act=edit&id=<?=$list['id']?>'" >编辑</button>
<button onclick="del(<?=$list['id']?>)">删除</button>
</td>
</tr>
<?php } ?>
</table>
<!-- 分页 -->
<p>
<?php
// 下一页
$next = ($page+1)>=$pages ? $pages : $page+1;
// 上一页
$preve = ($page-1)==0 ? 0 : $page-1;
?>
<a href="<?php echo $url."?p=1" ?>">首页</a>
<a href="<?php echo $url."?p=".$preve; ?>">上一页</a>
<?php
$html = '';
if($pages>10 && $page>5 && $page<$pages-5){
// 显示2边省略号
$html .= "<a href='".$url."?p=1' class='".$clas."' >1</a>";
$html .= "<a href='".$url."?p=2' class='".$clas."' >2</a>";
$html .="<span>......</span>";
for($i=$page-2;$i<=$page+2;$i++){
$clas = ($page==$i) ? "cla" : null;
$html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
}
$html .="<span>......</span>";
$html .= "<a href='".$url."?p=".($pages-1)."' class='".$clas."' >".($pages-1)."</a>";
$html .= "<a href='".$url."?p=".$pages."' class='".$clas."' >".$pages."</a>";
}elseif($pages>10 && $page<=5){
// 显示后面省略号
for($i=1;$i<=10;$i++){
$clas = ($page==$i) ? "cla" : null;
$html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
}
$html .="<span>......</span>";
$html .= "<a href='".$url."?p=".($pages-1)."' class='".$clas."' >".($pages-1)."</a>";
$html .= "<a href='".$url."?p=".$pages."' class='".$clas."' >".$pages."</a>";
}elseif($pages>10 && $page>=$pages-5){
// 显示前面省略号
$html .= "<a href='".$url."?p=1' class='".$clas."' >1</a>";
$html .= "<a href='".$url."?p=2' class='".$clas."' >2</a>";
$html .="<span>......</span>";
for($i=$pages-5;$i<=$pages;$i++){
$clas = ($page==$i) ? "cla" : null;
$html.="<a href='".$url."?p=".$i."' class='".$clas."' >".$i."</a>";
}
}else{
// 显示全部分页
for($i=1;$i<=$pages;$i++){
$html.="<a href='".$url."?p='".$i."' class='".$clas."' >".$i."</a>";
}
}
echo $html;
?>
<a href="<?php echo $url."?p=".$next; ?>">下一页</a>
<a href="<?php echo $url."?p=".$pages; ?>">尾页</a>
</p>
</body>
<script>
function del(id){
var y= confirm("是否删除");
if(y==true){
location.href="control.php?act=del&id="+id;
}
}
</script>
</html>
3.演示图: