批改状态:合格
老师批语:
<?phpspl_autoload_register(function($className){// $className 是new的Son这个名字,这个名字是类名,不是文件名require $className . '.php';});
<?phpclass CreatePDO{public static function Create(){try{$pdo = new PDO('mysql:host=127.0.0.1;dbname=prodb','root','123456');}catch(PDOException $e){// 抛出错误,错误是你可以定义的echo '数据库连接失败' . $e->getMessage();}return $pdo;}}
<?phpclass User{public $id;public $account;public $password;public $name;public $phone;public $email;public $createtime;public $updatetime;public $state;}
<?phprequire_once "auto.php";class UserContr{/***添加用户*/public static function createUser($user){$sql="INSERT INTO `php_user` SET `account`=? ,`password`=?,";$sql.="`name`=?,`email`=?,`phone`=?,`createtime`=?,`updatetime`=?,`state`=?";$pdo =CreatePDO::Create();$pre=$pdo->prepare($sql);$exec=$pre->execute([$user->account,$user->password,$user->name,$user->email,$user->phone,$user->createTime,$user->updateTime,$user->state]);if($exec){return 1;}else{return 0;}}// 根据id查询用户信息public static function GetUserById($id){$sql="SELECT * FROM `php_user` WHERE `id`=$id";$pdo =CreatePDO::Create();$pre=$pdo->prepare($sql);$exec=$pre->execute();$data = $pre -> fetch();return $data;}// 所有用户信息public static function GetAllUser(){$sql="SELECT * FROM `php_user`";$pdo =CreatePDO::Create();$pre=$pdo->prepare($sql);$exec=$pre->execute();$data = $pre -> fetchAll(PDO::FETCH_OBJ);return $data;}// 根据id删除用户信息public static function DeleteById($id){$pdo =CreatePDO::Create();$sql="DELETE FROM `php_user` WHERE id=$id";$pre=$pdo->prepare($sql);$exec=$pre->execute();if($exec){$res=1;}else{$res=0;}return $res;}// 修改用户信息public static function UpdateUser($user){$pdo =CreatePDO::Create();$sql="UPDATE `php_user` SET `name`=?,`email`=?,`phone`=?,`updatetime`=?,`state`=? WHERE id=?";$pre=$pdo->prepare($sql);$exec=$pre->execute([$user->name,$user->email,$user->phone,$user->updateTime,$user->state,$user->id]);if($exec){return 1;}else{return 0;}}}
<!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>.form {width: 360px;margin: 0 auto;padding: 10px;}form {display: grid;gap: 0.5rem;}fieldset {display: grid;gap: 0.5rem;}h2 {text-align: center;}label {margin-left: 40px;}</style></head><body><div class="form"><h2>添加用户</h2><form action="createuser.php" method="post"><fieldset><legend>必填项</legend><div><label for="account">用名:</label><input id="account" type="text" name="account" required /></div><div><label for="password">密码:</label><input type="password" id="password" name="password" required /></div></fieldset><fieldset><legend>选填项</legend><div><label for="name">姓名:</label><input type="text" id="name" name="name" /></div><div><label for="email">邮箱:</label><input id="email" type="email" name="email" /></div><div><label for="phone">电话:</label><input id="phone" type="number" name="phone" /></div></fieldset><div><label for="phone">状态:</label><select name="state" id=""><option value="1">开启</option><option value="2">关闭</option></select></div><button>添加</button></form></div></body></html>
添加用户信息后台文件createuser.php
<?phprequire_once "auto.php";if(!empty($_POST)){$user=new User();$user->account=trim($_POST['account']);$user->password=md5(trim($_POST['password']));$user->name=trim($_POST['name'])??"";$user->email=trim($_POST['email'])??"";$user->phone=$_POST['phone'];$user->createTime=time();$user->updateTime=time();$user->state=$_POST['state'];if(UserContr::createUser($user)){echo '<script>alert("添加成功");location.href="showuser.php"</script>';}else{print_r($pre->errorInfo());}}
<!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>/* 1. 添加表格线,添加给单元格 */tbody td,th {border: 1px solid;}/* 2. 折叠表格线 */table {border-collapse: collapse;width: 90%;/* 对齐 */text-align: center;margin: 2em auto;}/* 标题 */table caption {font-weight: bolder;margin-bottom: 1em;}table thead {background-color: lightgreen;}tbody tr:nth-of-type(2n){background-color: #ddd;}</style></head><body><?phprequire_once "auto.php";$data=UserContr::GetAllUser();?><div class="show"><table><caption>用户信息</caption><thead><tr><th><input type="checkbox" id="check_all"></th><th>ID</th><th>用户名</th><th>姓名</th><th>电话</th><th>邮箱</th><th>创建时间</th><th>最后登录时间</th><th>状态</th></tr></thead><tbody><?php foreach($data as $user): ?><tr><td><input type="checkbox" class="chk_item"></td><td class="id"><?=$user->id?></td><td><a href="edituser.html?id=<?=$user->id?>" title="点击修改用户信息" ><?=$user->account?></a></td><td><?=$user->name?></td><td><?=$user->phone?></td><td><?=$user->email?></td><td><?=date('Y-m-d',$user->createtime)?></td><td><?=date('Y-m-d',$user->updatetime)?></td><td><?=$user->state==1?'开启':'关闭'?></td></tr><?php endforeach ?></tbody><tfoot><tr><td colspan="3"><button onclick="del()">删除</button></td><td><a href="adduser.html">添加用户</a></td></tr></tfoot></table></div><script>const chkAll=document.querySelector("#check_all");const chkItems=document.querySelectorAll(".chk_item");chkAll.onchange=ev=>chkItems.forEach(item=>item.checked=ev.target.checked);chkItems.forEach(item=>item.onchange=()=>chkAll.checked=[...chkItems].every(item=>item.checked));function del(){if(confirm('您确定要删除这些用户吗?')){const chkItems=document.querySelectorAll(".chk_item");chkItems.forEach(item=>{if(item.checked){const id=item.parentElement.nextElementSibling.textContent;let isDel=delOpt(id);if(isDel){// console.log( item.parentElement.parentElement);item.parentElement.parentElement.remove();}}})}else{alert("NO");}}async function delOpt(id){const response = await fetch("del_user.php?id=" + id);const comments = await response.json();return comments;}</script></body></html>
<?phprequire_once "auto.php";$id=$_GET['id'];$res=UserContr::DeleteById($id);echo $res;
<!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>.form {width: 360px;margin: 0 auto;padding: 10px;}form {display: grid;gap: 0.5rem;}fieldset {display: grid;gap: 0.5rem;}h2 {text-align: center;}</style></head><body><div class="form"><h2>更新用户信息</h2><form action="updateuser.php" method="post"><fieldset><legend>用户信息</legend><div><input type="hidden" name="id" /><label for="account">用名:</label><input id="account" type="text" name="account" required readonly /></div><div><label for="name">姓名:</label><input type="text" id="name" name="name" /></div><div><label for="email">邮箱:</label><input id="email" type="email" name="email" /></div><div><label for="phone">电话:</label><input id="phone" type="number" name="phone" /></div></fieldset><div><label for="phone">状态:</label><select name="state" id=""><option value="1">开启</option><option value="2">关闭</option></select></div><button>修改</button></form></div><script>let url = location.search;let id = url.substr(4);if (id > 0) {fetch("searchbyid.php?id=" + id).then((response) => response.json()).then((json) => {const form = document.forms[0];form.id.value = json.id;form.account.value = json.account;form.account.readonly = true;form.phone.value = json.phone;form.email.value = json.email;form.name.value = json.name;form.state.value = json.state;});} else {}</script></body></html>
<?phprequire_once "auto.php";if(!empty($_POST)){$user=new User();$user->id=$_POST['id'];$user->account=trim($_POST['account']);$user->name=trim($_POST['name'])??"";$user->email=trim($_POST['email'])??"";$user->phone=$_POST['phone'];$user->updateTime=time();$user->state=$_POST['state'];$res=UserContr::UpdateUser($user);if($res){echo '<script>alert("修改成功");location.href="showuser.php"</script>';}else{}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号