Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
文件 users.php 代码:
<?php
// 用户类
class User{
public $phone;
public $pwd;
public $name;
public $age;
public $status;
//登录
public function login($uphone,$upwd){
require('db.php');
$sql = "select * from users where u_phone = '".$uphone."' and u_pwd ='".md5($upwd)."'";
$stmt = $pdo -> prepare($sql);
$res = $stmt -> execute();
if ($res) {
$row = $stmt -> fetch();
if (!empty($row)) {
return array('code'=>1,'msg'=>"登录成功",'res'=>$row);
}else {
return array('code'=>0,'msg'=>'登录失败');
};
};
}
//获取用户信息
public function getUser($id){
require('db.php');
$sql = "select * from users where u_id = ".$id."";
$stmt = $pdo -> prepare($sql);
$res = $stmt -> execute();
if ($res) {
$row = $stmt -> fetch();
if (!empty($row)) {
return array('code'=>1,'msg'=>"成功",'res'=>$row);
}else {
return array('code'=>0,'msg'=>'失败');
};
};
}
//获取用户列表
public function getUserList(){
require('db.php');
$sql = "select * from users";
$stmt = $pdo -> prepare($sql);
$res = $stmt -> execute();
if ($res) {
$row = $stmt -> fetchAll();
if (!empty($row)) {
return array('code'=>1,'msg'=>"成功",'res'=>$row);
}else {
return array('code'=>0,'msg'=>'失败');
};
};
}
//注册
public function add($uphone,$upwd,$uname,$uage){
require('db.php');
$sql = "insert into `users`(`u_phone`,`u_pwd`,`u_name`,`u_age`,`u_createtime`) values('".$uphone."','".md5($upwd)."','".$uname."',".$uage.",".time().")";
$stmt = $pdo -> prepare($sql);
$stmt -> execute();
if ($stmt -> rowCount() > 0) {
return array('code'=>1,'msg'=>"注册成功");
}else {
return array('code'=>0,'msg'=>'注册失败');
};
}
//修改
public function update($id,$name,$age,$pwd,$status){
require('db.php');
if (!empty($pwd)) {
$sql = "update `users` set `u_name`='".$name."',`u_age`=".$age.",`u_pwd`= '".md5($pwd)."',`u_status`=".$status." where `u_id` = ".$id.";";
}else {
$sql = "update `users` set `u_name`='".$name."',`u_age`=".$age.",`u_status`=".$status." where `u_id` = ".$id.";";
}
$stmt = $pdo -> prepare($sql);
$stmt -> execute();
if ($stmt -> rowCount() > 0) {
return array('code'=>1,'msg'=>"修改成功");
}else {
return array('code'=>0,'msg'=>'修改失败');
};
}
//删除
public function delete($uid){
require('db.php');
$sql = "delete from `users` where `u_id` = ".$uid;
echo $sql;
$stmt = $pdo -> prepare($sql);
$stmt -> execute();
if ($stmt -> rowCount() > 0) {
return array('code'=>1,'msg'=>"删除成功");
}else {
return array('code'=>0,'msg'=>'删除失败');
};
}
}
?>
文件 reg.php 代码:
<?php
require_once('users.php');
if (isset($_POST['submit'])) {
$user = new User();
$u = $user->add($_POST['phone'],$_POST['pwd'],$_POST['name'],$_POST['age']);
if ($u['code']) {
print_r($u);
echo "<script>alert('".$u['msg']."');location.href='login.php'</script>";
} else {
echo "<script>alert('".$u['msg']."');</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>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="jumbotron">
<h1 class="display-4">用户注册</h1>
<hr class="my-4">
<form method="POST" action="" >
<div class="form-group">
<label>姓名</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label>手机号</label>
<input class="form-control" type="text" name="phone">
</div>
<div class="form-group">
<label>密码</label>
<input class="form-control" type="password" name="pwd">
</div>
<div class="form-group">
<label>年龄</label>
<input class="form-control" type="number" name="age">
</div>
<hr class="my-4">
<button type="submit" name="submit" class="btn btn-primary">立即注册</button>
<a href="login.php" class="btn btn-success">登录</a>
</form>
</div>
</body>
</html>
文件 login.php 代码:
<?php
require_once('users.php');
if (!empty($_POST['phone'])) {
$user = new User();
$u = $user->login($_POST['phone'],$_POST['pwd']);
if ($u['code']) {
echo "<script>alert('".$u['msg']."');location.href='list.php'</script>";
} else {
echo "<script>alert('".$u['msg']."');</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>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="jumbotron">
<h1 class="display-4">登录</h1>
<hr class="my-4">
<form method="POST" action="" >
<div class="form-group">
<label>手机号</label>
<input class="form-control" type="text" name="phone">
<small class="form-text text-muted">请输入手机号码</small>
</div>
<div class="form-group">
<label>密码</label>
<input class="form-control" type="password" name="pwd">
</div>
<button type="submit" class="btn btn-primary">登录</button>
<a href="reg.php" class="btn btn-success">注册</a>
</form>
</div>
</body>
</html>
文件 list.php 代码:
<?php
require_once('users.php');
$user = new User();
$u = $user->getUserList();
if (!empty($_GET['id'])) {
$res = $user -> delete($_GET['id']);
if ($res['code'] === 1) {
echo "<script>alert('".$res['msg']."');location.href='list.php'</script>";
} else {
echo "<script>alert('".$res['msg']."');</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>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="jumbotron">
<h1 class="display-4">用户列表</h1>
<hr class="my-4">
<table class="table">
<thead class="thead-dark">
<tr>
<th>ID</th>
<th>手机号</th>
<th>用户名</th>
<th>年龄</th>
<th>注册日期</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($u['res'] as $user) { ?>
<tr>
<td><?=$user['u_id']?></td>
<td><?=$user['u_phone']?></td>
<td><?=$user['u_name']?></td>
<td><?=$user['u_age']?></td>
<td><?=date('Y-m-d',$user['u_createtime'])?></td>
<td><?=$user['u_status'] == 0?'<span class="badge badge-warning">待审</span>':'<span class="badge badge-success">正常</span>'?></td>
<td>
<a href="edit.php?id=<?=$user['u_id']?>" class="btn btn-primary btn-sm">修改</a>
<a href="list.php?id=<?=$user['u_id']?>" class="btn btn-danger btn-sm">删除</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
文件 edit.php 代码:
<?php
require_once('users.php');
$id = $_GET['id'];
$info = array();
if (!empty($id)) {
$user = new User();
//读取用户信息
$uinfo = $user -> getUser($id);
if ($uinfo['code'] == 1) {
$info = $uinfo['res'];
}
//提交修改
if(isset($_POST['submit'])){
$status = $_POST['status'];
$u = $user-> update($id,$_POST['name'],$_POST['age'],$_POST['pwd'],$status == 'on'? $status=1:$status=0);
if ($u['code']) {
echo "<script>alert('".$u['msg']."');location.href='list.php'</script>";
} else {
echo "<script>alert('".$u['msg']."');</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>
<link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div class="jumbotron">
<h1 class="display-4">编辑用户信息</h1>
<hr class="my-4">
<form method="POST" action="" >
<div class="form-group">
<label>姓名</label>
<input class="form-control" type="text" name="name" value="<?=$info['u_name']?>">
</div>
<div class="form-group">
<label>手机号</label>
<input class="form-control" type="text" name="phone" value="<?=$info['u_phone']?>" readonly>
</div>
<div class="form-group">
<label>密码</label>
<input class="form-control" placeholder="不填为不修改" type="password" name="pwd" value="">
</div>
<div class="form-group">
<label>年龄</label>
<input class="form-control" type="number" name="age" value="<?=$info['u_age']?>">
</div>
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" name="status" <?=$info['u_status']==1?'checked':''?> id="status">
<label class="custom-control-label" for="status">状态</label>
</div>
</div>
<hr class="my-4">
<button type="submit" name="submit" class="btn btn-primary">提交修改</button>
</form>
</div>
</body>
</html>