批改状态:合格
老师批语:不错
作业内容:1、练习课上代码2、看看函数库3、预习类4、修改用户功能(根据添加来改)5、删除用户功能
先上图



FileName : index.php(列表)
<?phprequire_once 'Mypdo.class.php';$pdo = new Mypdo("mysql", "localhost", "study", "root", "root");$pdo->connect();$res = $pdo->select("user","*","","id","0,10");?><!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>table{width: 100%;border-collapse:collapse;}th, td{padding: 5px;border: 1px solid #336699;}th{font-size: 20px;font-weight: bolder;background: #ccc;}</style></head><body><h2>用户列表</h2><hr/><button style="margin-bottom:8px;" onclick="add();">添加用户</button><table><tr><th>ID</th><th>帐号</th><th>姓名</th><th>年龄</th><th>电话</th><th>创建时间</th><th>最后登陆时间</th><th>状态</th><th>操作</th></tr><?php foreach($res as $k=>$v){ ?><tr><td><?=$v['id'] ?></td><td><?=$v['account'] ?></td><td><?=$v['name'] ?></td><td><?=$v['age'] ?></td><td><?=$v['phone'] ?></td><td><?=date('Y-m-d', $v['add_time']) ?></td><td><?=date('Y-m-d', $v['last_time']) ?></td><td><?=$v['status']?"正常":"禁用" ?></td><td><a href="javascript:mod(<?=$v['id'] ?>);">修改</a> | <a href="javascript:del(<?=$v['id'] ?>);">删除</a></td></tr><?php } ?></table><script>//添加用户function add(){this.location = "add.php";}//修改用户function mod(id){this.location = "mod.php?id=" + id;}//删除用户function del(id){if(confirm("确定要删除该用户吗?")){this.location = "del.php?id=" + id;}else{return false;}}</script></body></html>
FileName: add.php(添加)
<!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>body{background: #eee;}div{padding: 2px;background: #ccc;width: 400px;height: 260px;position: absolute;left:50%;top:50%;margin-left:-200px;margin-top:-130px;border-radius:4px;box-shadow: 4px 4px 4px 4px #666;}div *{color:#336699;text-align: center;}input[type="submit"]{margin-top:8px;width: 220px;height: 30px;}</style></head><body><div><h1>添加用户</h1><form method="post">账户:<input name="account" type="text" /><br/>密码:<input name="password" type="password" /><br/>姓名:<input name="name" type="text" /><br/>年龄:<input name="age" type="number" /><br/>手机:<input name="phone" type="text" /><br/><input type="submit" value="添加" /></form></div></body></html><?php// 1、判断是否是post提交if(!empty($_POST)){// 2、账户必须填写,如果不填写,就会报错if(empty($_POST['account'])){echo '<div style="color:red;margin-top:20px;">请输入账户</div>';exit;}// 3、密码必须添加if(empty($_POST['password'])){echo '<div style="color:red;margin-top:20px;">请输入密码</div>';exit;}$_POST['password'] = md5($_POST['password']);$data = ["add_time" => time(),"last_time" => time(),"status" => 1];require_once 'Mypdo.class.php';$pdo = new Mypdo("mysql", "localhost", "study", "root", "root");$pdo->connect();if($pdo->insert("user", array_merge($data, $_POST))){echo '<script>alert("添加成功"); this.location="index.php"; </script>';}else{echo '<script>alert("添加失败");</script>';}}?>
FileName : mod.php(修改)
<?phpif(!isset($_GET['id'])){die("参数有误");}require_once 'Mypdo.class.php';$pdo = new Mypdo("mysql", "localhost", "study", "root", "root");$pdo->connect();$res = $pdo->find("user","*","id={$_GET['id']}");?><!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>body{background: #eee;}div{padding: 2px;background: #ccc;width: 400px;height: 260px;position: absolute;left:50%;top:50%;margin-left:-200px;margin-top:-130px;border-radius:4px;box-shadow: 4px 4px 4px 4px #666;}div *{color:#336699;text-align: center;}input[type="submit"]{margin-top:8px;width: 220px;height: 30px;}</style></head><body><div><h1>修改用户</h1><form method="post">账户:<input name="account" type="text" value="<?=$res['account'] ?>" /><br/>姓名:<input name="name" type="text" value="<?=$res['name'] ?>" /><br/>年龄:<input name="age" type="number" value="<?=$res['age'] ?>" /><br/>手机:<input name="phone" type="text" value="<?=$res['phone'] ?>" /><br/><select name="status" id="status"><?phpfor($i=0;$i<2;$i++){echo '<option value="'.$i.'" '.($i==$res['status']?" selected":"") .'>'.($i?"正常":"禁用").'</option>';}?></select><br><input type="submit" value="修改" /></form></div></body></html><?php// 1、判断是否是post提交if(!empty($_POST)){// 2、账户必须填写,如果不填写,就会报错if(empty($_POST['account'])){echo '<div style="color:red;margin-top:20px;">请输入账户</div>';exit;}require_once 'Mypdo.class.php';$pdo = new Mypdo("mysql", "localhost", "study", "root", "root");$pdo->connect();if($pdo->update("user", $_POST, "id=".$_GET['id'])){echo '<script>alert("修改成功"); this.location="index.php"; </script>';}else{echo '<script>alert("修改失败");</script>';}}?>
FileName:del.php(删除)
<?phprequire_once 'Mypdo.class.php';$pdo = new Mypdo("mysql", "localhost", "study", "root", "root");$pdo->connect();if($pdo->delete("user","id=".$_GET['id'])){echo '<script>alert("删除成功"); this.location="index.php"; </script>';}else{echo '<script>alert("删除失败");</script>';}?>
Mypdo.class.php(引用类库)
<?php/*** Pdo操作数据库类* 2021年10月19日*/class Mypdo{private $db_type; //数据库类型private $db_host; //数据库主机private $db_name; //数据库名private $db_user; //连接数据库帐号private $db_password; //连接数据库密码private $pdo; //连接句柄public function __construct($type, $host, $name, $user, $password, $charset = 'utf-8'){// 存储相关参数$this->db_type = $type;$this->db_host = $host;$this->db_name = $name;$this->db_user = $user;$this->db_password = $password;}public function connect(){$dsn = "{$this->db_type}:host={$this->db_host}; dbname={$this->db_name}";//数据源// echo $dsn;// exit;try {//实例化PDO类,创建PDO对象$this->pdo = new PDO($dsn,$this->db_user, $this->db_password);} catch (PDOException $e) {die('数据库错误:'.$e->getMessage());}return $this->pdo;}// 按指定条件,查询多条记录public function select($table, $fields='*', $where='', $order='', $limit=''){//创建SQL语句$sql = 'SELECT ';if (is_array($fields)) { // 支持数组foreach ($fields as $field) {$sql .= $field.', ';}} else { // 也支持字符串$sql .= $fields;}//去掉最后的豆号和空格$sql = rtrim(trim($sql),',');$sql .= ' FROM '.$table;//查询条件:暂只支持字符串if(!empty($where)){$sql .= ' WHERE '.$where;}//排序条件,暂未支持顺序和倒序,默认用倒序吧,新添加的放最前面if(!empty($order)) {$sql .= ' order by '.$order.' DESC';}//分页条件if(!empty($limit)) {$sql .= ' limit '.$limit;}$sql .= ';';//创建PDO预处理对象$stmt = $this->pdo->prepare($sql);//执行查询操作if($stmt->execute()){if($stmt->rowCount()>0){$stmt->setFetchMode(PDO::FETCH_ASSOC);//返回一个二维数组return $stmt->fetchAll();}} else {return false;}}// 查询单条记录public function find($table,$fields,$where=''){//创建SQL语句$sql = 'SELECT ';if (is_array($fields)) {foreach ($fields as $field) {$sql .= $field.', ';}} else {$sql .= $fields;}$sql = rtrim(trim($sql),',');$sql .= ' FROM '.$table;//查询条件if(!empty($where)){$sql .= ' WHERE '.$where;}$sql .= ' LIMIT 1;';//创建PDO预处理对象$stmt = $this->pdo->prepare($sql);//执行查询操作if($stmt->execute()){if($stmt->rowCount()>0){$stmt->setFetchMode(PDO::FETCH_ASSOC);return $stmt->fetch();}} else {return false;}}//插入数据public function insert($table,$data=[]){//创建SQL语句$sql = "INSERT INTO {$table} SET ";//组装插入语句if(is_array($data)){foreach ($data as $k=>$v) {$sql .= $k.'="'.$v.'", ';}}else{return false;}//去掉尾部逗号,并添加分号结束$sql = rtrim(trim($sql),',').';';//创建PDO预处理对象$stmt = $this->pdo->prepare($sql);//执行新增操作if($stmt->execute()){if($stmt->rowCount()>0){return true;}} else {return false;}}// 更新数据public function update($table,$data=[], $where='') {//创建SQL语句$sql = "UPDATE {$table} SET ";//组装修改语句if(is_array($data)){foreach ($data as $k=>$v) {$sql .= $k.'="'.$v.'", ';}}//去掉尾部逗号,并添加分号结束$sql = rtrim(trim($sql),',');//查询条件if(!empty($where)){$sql .= ' WHERE '.$where;}//创建PDO预处理对象$stmt = $this->pdo->prepare($sql);//执行新增操作if($stmt->execute()){if($stmt->rowCount()>0){return true;}} else {return false;}}// 删除数据public function delete($table,$where=''){//创建SQL语句$sql = "DELETE FROM {$table} ";//查询条件if(!empty($where)){$sql .= ' WHERE '.$where;}else{ //加上这一句。以防没有条件,删除了所有数据return false;}//创建PDO预处理对象$stmt = $this->pdo->prepare($sql);//执行删除操作if($stmt->execute()){if($stmt->rowCount()>0){return true;}} else {return false;}}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号