Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
工具类: DB.php
<?php
class DB{
public static $config = array(); //数据库连接的配置信息
public static $pdo = null; //PDO连接标识符
public static $stmt = null; //PDOStatement类。预处理sql语句及存放结果集
public static $sql = null; //最后执行的sql语句
public static $lastInsertId = null;//最后一次插入操作的自增值(AUTO_INCREMANT)
public static $error=null; //错误信息
/**
* 构造函数, 实例化时用来连接数据库
* @param array 数据库配置项
*/
public function __construct($dbConfig=''){
//实例化时,如果未传参数或参数非数组格式则连接默认数据库
if(!is_array($dbConfig)){
$dbConfig = [
'dbms'=>'mysql',
'host'=>'10.10.1.242',
'dbname'=>'php',
'user'=>'root',
'password'=>'130599',
'charset'=>'utf8'
];
}
//判断是否配置了host项(数据库)
if(empty($dbConfig['host'])){
self::throw_exception("没有配置数据库,请先配置!");
}
self::$config = $dbConfig;
if(!isset(self::$pdo)){
try{
if(self::$config['dbms']=='mysql'){
$dsn = 'mysql:host=' . self::$config['host'] . ';dbname=' . self::$config['dbname'];
}else if(self::$config['dbms']=='sqlsrv'){
$dsn = 'sqlsrv:server=' . self::$config['host'] . ';database=' . self::$config['dbname'];
}
self::$pdo = new PDO($dsn,self::$config['user'],self::$config['password']);
}catch(PDOException $e){
self::throw_exception($e->getMessage());
}
}
if(!self::$pdo){
self::throw_exception("PDO连接错误!");
return false;
}
}
/**
*获取单条记录
*@param string 要执行的sql语句
*@return array 结果集(一维数组)
*/
public static function fetch($sql=null){
if($sql!=null)self::query($sql);
$res = self::$stmt->fetch(constant("PDO::FETCH_ASSOC"));
return $res;
}
/**
* 获取所有记录
* @param string 要执行的sql语句
* @return array 结果集(二维数组)
*/
public static function fetchAll($sql=null){
if($sql!=null)self::query($sql);
$res = self::$stmt->fetchAll(constant("PDO::FETCH_ASSOC"));
return $res;
}
/**
* 执行查询
* @param 要查询的sql语句
* @return boolean
*/
public static function query($sql=''){
if(!self::$pdo)return false; //如果PDO未连接则返回
if(!empty(self::$stmt))self::free(); //如果$stmt之前有结果集则先释放
self::$sql = $sql;
self::$stmt = self::$pdo->prepare(self::$sql);
$res = self::$stmt->execute();
self::haveErrorThrowException();
return $res;
}
/**
* 执行增删改操作,返回受影响的行数
* @param string 要执行的sql语句
* @return number 受影响的行数 *
*/
public static function execute($sql=null){
if(!self::$pdo)return false;//如果PDO未连接则返回
if(!empty(self::$stmt))self::free(); //如果$stmt之前有结果集则先释放
if($sql!=null)self::$sql = $sql;
$res = self::$pdo->exec($sql);
self::haveErrorThrowException();
if($res){
self::$lastInsertId = self::$pdo->lastInsertId();
return $res;
}else{
return false;
}
}
/**
* 释放结果集
*/
public static function free(){
self::$stmt = null;
}
/**
* 自定义错误消息
* @param string 错误消息
*/
public static function throw_exception($msg){
echo $msg;
}
/**
* 抛出错误信息
*
* @return boolean ( description_of_the_return_value )
*/
public static function haveErrorThrowException(){
$obj = empty(self::$stmt) ? self::$pdo : self::$stmt;
$arrError = $obj->errorInfo();
if($arrError[0]!='00000'){
self::$error = 'SQLSTATE=>'.$arrError[0].'<br/>SQL Error=>'.$arrError[2].'<br/>Error SQL=>'.self::$sql;
self::throw_exception(self::$error);
return false;
}
if(self::$sql==''){
self::throw_exception('没有可执行的SQL语句');
return false;
}
}
}
列表页: user_list.php
<?php
require_once 'auto.php';
$db = new DB();
$data = DB::fetchAll('select * from user');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>用户列表</title>
<style>
.list {
margin:1em auto;
border-collapse: collapse;
}
.title{
text-align:center;
margin-top:2em;
}
.list th,
.list td {
border: 1px solid black;
font-size:14px;
padding: 2px 5px;
}
.list a{
background-color: #f3f3f3;
border-radius:10px;
padding:0 3px;
}
</style>
</head>
<body>
<h2 class="title">用户列表</h2>
<table class="list">
<thead>
<tr>
<th>id</th>
<th>帐号</th>
<th>姓名</th>
<th>年龄</th>
<th>手机号码</th>
<th>注册时间</th>
<th>登录时间</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $row){
?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['account'] ?></td>
<td><?= $row['name'] ?></td>
<td><?= $row['age'] ?></td>
<td><?= $row['phone'] ?></td>
<td><?= $row['add_time'] ?></td>
<td><?= $row['last_time'] ?></td>
<td><?= $row['status'] ?></td>
<td><a href="user_curd.php?type=m&id=<?= $row['id'] ?>">修改</a> <a href="user_curd.php?type=d&id=<?= $row['id'] ?>">删除</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
修改删除操作页: user_curd.php
<?php
require_once 'auto.php';
$type = $_REQUEST['type'];
$id = $_REQUEST['id'];
if(empty($_POST)){ //GET
if($type == 'm'){ //修改
$db = new DB();
$data = DB::fetch("select * from user where id = " . $id);
}else if($type == 'd'){ //删除
$db = new DB();
$res = DB::execute("delete from user where id = " . $id);
if($res){
echo "<script>alert('删除成功!');window.location='user_list.php';</script>";
}else{
echo "<script>alert('没有任何数据被修改!');window.location='user_list.php';</script>";
}
return;
}else{
echo '无效的请求!';
return false;
}
}else{
//POST
$data['account'] = trim($_POST['account']);
$data['password'] = trim($_POST['pwd']);
$data['name'] = trim($_POST['username']);
$data['age'] = trim($_POST['age']);
$data['phone'] = trim($_POST['phone']);
$data['add_time'] = strtotime($_POST['atime']);
$data['last_time'] = strtotime($_POST['ltime']);
$data['status'] = $_POST['status'];
if(empty($data['account']) or empty($data['password'])){
echo '帐号或密码不能为空!';
}else{
//拼接sql语句
$sql = "update user set ";
foreach($data as $key=>$value){
$sql .= $key . " = '" . $value . "',";
}
$sql = rtrim($sql,',');
$sql .= " where id = " . $id;
//执行修改操作
$db = new DB();
$res = DB::execute($sql);
if($res){
// header("Refresh:2;url=user_list.php");
echo "<script>alert('修改成功!即将返回用户列表页..');window.location='user_list.php';</script>";
}else{
echo "<script>alert('修改失败!');</script>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>用户列表</title>
<style>
* {
box-sizing: border-box;
}
.box {
width: 400px;
background-color: #f9f9f9;
margin: 0 auto;
border-radius: 10px;
}
.form {
width: 400px;
padding: 1em 1.5em 0.5em 1em;
display: grid;
grid-template-columns: 25% 75%;
gap: 0.5em;
}
.title {
text-align: center;
margin-top: 1em;
}
</style>
</head>
<body>
<h2 class="title">用户信息修改</h2>
<div class="box">
<form action="" method="post" class="form">
<label>id</label>
<input type="text" value="<?= $data['id'] ?>" name="id" readonly disabled />
<label for="account">帐号</label>
<input
type="text"
name="account"
id="account"
required
placeholder="帐号不为空..."
value="<?= $data['account'] ?>"
/>
<label for="pwd">密码</label>
<input
type="password"
name="pwd"
id="pwd"
required
placeholder="密码不能为空..."
value="<?= $data['password'] ?>"
/>
<label for="username">姓名</label>
<input type="text" name="username" id="username" value="<?= $data['name'] ?>" />
<label for="age">年龄</label>
<input type="text" name="age" id="age" value="<?= $data['age'] ?>" />
<label for="phone">手机号码</label>
<input type="text" name="phone" id="phone" value="<?= $data['phone'] ?>" />
<label for="atime">注册时间</label>
<input type="text" name="atime" id="atime" value="<?= date('Y-m-d',$data['add_time']) ?>"/>
<label for="ltime">登录时间</label>
<input type="text" name="ltime" id="ltime" value="<?= date('Y-m-d H:i:s',$data['last_time']) ?>"/>
<label for="status">状态</label>
<select name="status" id="status">
<option value="1" <?= $data['status'] == '1' ? 'selected' : ''; ?> >开启</option>
<option value="0" <?= $data['status'] == '0' ? 'selected' : ''; ?> >关闭</option>
</select>
<label for=""></label>
<input type="submit" value="提交" />
</form>
</div>
</body>
</html>
修改操作截图:
删除操作截图: