Blogger Information
Blog 14
fans 0
comment 0
visits 9470
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
初识 类与对象
Mr.Ran
Original
483 people have browsed it

1.创建一个用户类

文件 users.php 代码:

  1. <?php
  2. // 用户类
  3. class User{
  4. public $phone;
  5. public $pwd;
  6. public $name;
  7. public $age;
  8. public $status;
  9. //登录
  10. public function login($uphone,$upwd){
  11. require('db.php');
  12. $sql = "select * from users where u_phone = '".$uphone."' and u_pwd ='".md5($upwd)."'";
  13. $stmt = $pdo -> prepare($sql);
  14. $res = $stmt -> execute();
  15. if ($res) {
  16. $row = $stmt -> fetch();
  17. if (!empty($row)) {
  18. return array('code'=>1,'msg'=>"登录成功",'res'=>$row);
  19. }else {
  20. return array('code'=>0,'msg'=>'登录失败');
  21. };
  22. };
  23. }
  24. //获取用户信息
  25. public function getUser($id){
  26. require('db.php');
  27. $sql = "select * from users where u_id = ".$id."";
  28. $stmt = $pdo -> prepare($sql);
  29. $res = $stmt -> execute();
  30. if ($res) {
  31. $row = $stmt -> fetch();
  32. if (!empty($row)) {
  33. return array('code'=>1,'msg'=>"成功",'res'=>$row);
  34. }else {
  35. return array('code'=>0,'msg'=>'失败');
  36. };
  37. };
  38. }
  39. //获取用户列表
  40. public function getUserList(){
  41. require('db.php');
  42. $sql = "select * from users";
  43. $stmt = $pdo -> prepare($sql);
  44. $res = $stmt -> execute();
  45. if ($res) {
  46. $row = $stmt -> fetchAll();
  47. if (!empty($row)) {
  48. return array('code'=>1,'msg'=>"成功",'res'=>$row);
  49. }else {
  50. return array('code'=>0,'msg'=>'失败');
  51. };
  52. };
  53. }
  54. //注册
  55. public function add($uphone,$upwd,$uname,$uage){
  56. require('db.php');
  57. $sql = "insert into `users`(`u_phone`,`u_pwd`,`u_name`,`u_age`,`u_createtime`) values('".$uphone."','".md5($upwd)."','".$uname."',".$uage.",".time().")";
  58. $stmt = $pdo -> prepare($sql);
  59. $stmt -> execute();
  60. if ($stmt -> rowCount() > 0) {
  61. return array('code'=>1,'msg'=>"注册成功");
  62. }else {
  63. return array('code'=>0,'msg'=>'注册失败');
  64. };
  65. }
  66. //修改
  67. public function update($id,$name,$age,$pwd,$status){
  68. require('db.php');
  69. if (!empty($pwd)) {
  70. $sql = "update `users` set `u_name`='".$name."',`u_age`=".$age.",`u_pwd`= '".md5($pwd)."',`u_status`=".$status." where `u_id` = ".$id.";";
  71. }else {
  72. $sql = "update `users` set `u_name`='".$name."',`u_age`=".$age.",`u_status`=".$status." where `u_id` = ".$id.";";
  73. }
  74. $stmt = $pdo -> prepare($sql);
  75. $stmt -> execute();
  76. if ($stmt -> rowCount() > 0) {
  77. return array('code'=>1,'msg'=>"修改成功");
  78. }else {
  79. return array('code'=>0,'msg'=>'修改失败');
  80. };
  81. }
  82. //删除
  83. public function delete($uid){
  84. require('db.php');
  85. $sql = "delete from `users` where `u_id` = ".$uid;
  86. echo $sql;
  87. $stmt = $pdo -> prepare($sql);
  88. $stmt -> execute();
  89. if ($stmt -> rowCount() > 0) {
  90. return array('code'=>1,'msg'=>"删除成功");
  91. }else {
  92. return array('code'=>0,'msg'=>'删除失败');
  93. };
  94. }
  95. }
  96. ?>

2.注册用户

文件 reg.php 代码:

  1. <?php
  2. require_once('users.php');
  3. if (isset($_POST['submit'])) {
  4. $user = new User();
  5. $u = $user->add($_POST['phone'],$_POST['pwd'],$_POST['name'],$_POST['age']);
  6. if ($u['code']) {
  7. print_r($u);
  8. echo "<script>alert('".$u['msg']."');location.href='login.php'</script>";
  9. } else {
  10. echo "<script>alert('".$u['msg']."');</script>";
  11. }
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20. <title>注册</title>
  21. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
  22. </head>
  23. <body>
  24. <div class="jumbotron">
  25. <h1 class="display-4">用户注册</h1>
  26. <hr class="my-4">
  27. <form method="POST" action="" >
  28. <div class="form-group">
  29. <label>姓名</label>
  30. <input class="form-control" type="text" name="name">
  31. </div>
  32. <div class="form-group">
  33. <label>手机号</label>
  34. <input class="form-control" type="text" name="phone">
  35. </div>
  36. <div class="form-group">
  37. <label>密码</label>
  38. <input class="form-control" type="password" name="pwd">
  39. </div>
  40. <div class="form-group">
  41. <label>年龄</label>
  42. <input class="form-control" type="number" name="age">
  43. </div>
  44. <hr class="my-4">
  45. <button type="submit" name="submit" class="btn btn-primary">立即注册</button>
  46. <a href="login.php" class="btn btn-success">登录</a>
  47. </form>
  48. </div>
  49. </body>
  50. </html>

3.登录

文件 login.php 代码:

  1. <?php
  2. require_once('users.php');
  3. if (!empty($_POST['phone'])) {
  4. $user = new User();
  5. $u = $user->login($_POST['phone'],$_POST['pwd']);
  6. if ($u['code']) {
  7. echo "<script>alert('".$u['msg']."');location.href='list.php'</script>";
  8. } else {
  9. echo "<script>alert('".$u['msg']."');</script>";
  10. }
  11. }
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <title>登录</title>
  20. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
  21. </head>
  22. <body>
  23. <div class="jumbotron">
  24. <h1 class="display-4">登录</h1>
  25. <hr class="my-4">
  26. <form method="POST" action="" >
  27. <div class="form-group">
  28. <label>手机号</label>
  29. <input class="form-control" type="text" name="phone">
  30. <small class="form-text text-muted">请输入手机号码</small>
  31. </div>
  32. <div class="form-group">
  33. <label>密码</label>
  34. <input class="form-control" type="password" name="pwd">
  35. </div>
  36. <button type="submit" class="btn btn-primary">登录</button>
  37. <a href="reg.php" class="btn btn-success">注册</a>
  38. </form>
  39. </div>
  40. </body>
  41. </html>

4.用户列表&删除用户

文件 list.php 代码:

  1. <?php
  2. require_once('users.php');
  3. $user = new User();
  4. $u = $user->getUserList();
  5. if (!empty($_GET['id'])) {
  6. $res = $user -> delete($_GET['id']);
  7. if ($res['code'] === 1) {
  8. echo "<script>alert('".$res['msg']."');location.href='list.php'</script>";
  9. } else {
  10. echo "<script>alert('".$res['msg']."');</script>";
  11. }
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20. <title>用户列表</title>
  21. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
  22. </head>
  23. <body>
  24. <div class="jumbotron">
  25. <h1 class="display-4">用户列表</h1>
  26. <hr class="my-4">
  27. <table class="table">
  28. <thead class="thead-dark">
  29. <tr>
  30. <th>ID</th>
  31. <th>手机号</th>
  32. <th>用户名</th>
  33. <th>年龄</th>
  34. <th>注册日期</th>
  35. <th>状态</th>
  36. <th>操作</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php foreach ($u['res'] as $user) { ?>
  41. <tr>
  42. <td><?=$user['u_id']?></td>
  43. <td><?=$user['u_phone']?></td>
  44. <td><?=$user['u_name']?></td>
  45. <td><?=$user['u_age']?></td>
  46. <td><?=date('Y-m-d',$user['u_createtime'])?></td>
  47. <td><?=$user['u_status'] == 0?'<span class="badge badge-warning">待审</span>':'<span class="badge badge-success">正常</span>'?></td>
  48. <td>
  49. <a href="edit.php?id=<?=$user['u_id']?>" class="btn btn-primary btn-sm">修改</a>
  50. <a href="list.php?id=<?=$user['u_id']?>" class="btn btn-danger btn-sm">删除</a>
  51. </td>
  52. </tr>
  53. <?php } ?>
  54. </tbody>
  55. </table>
  56. </div>
  57. </body>
  58. </html>

5.编辑用户

文件 edit.php 代码:

  1. <?php
  2. require_once('users.php');
  3. $id = $_GET['id'];
  4. $info = array();
  5. if (!empty($id)) {
  6. $user = new User();
  7. //读取用户信息
  8. $uinfo = $user -> getUser($id);
  9. if ($uinfo['code'] == 1) {
  10. $info = $uinfo['res'];
  11. }
  12. //提交修改
  13. if(isset($_POST['submit'])){
  14. $status = $_POST['status'];
  15. $u = $user-> update($id,$_POST['name'],$_POST['age'],$_POST['pwd'],$status == 'on'? $status=1:$status=0);
  16. if ($u['code']) {
  17. echo "<script>alert('".$u['msg']."');location.href='list.php'</script>";
  18. } else {
  19. echo "<script>alert('".$u['msg']."');</script>";
  20. }
  21. }
  22. }else {
  23. echo "<script>alert('请选择用户');</script>";
  24. }
  25. ?>
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8">
  30. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <title>修改用户信息</title>
  33. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css" rel="stylesheet" />
  34. </head>
  35. <body>
  36. <div class="jumbotron">
  37. <h1 class="display-4">编辑用户信息</h1>
  38. <hr class="my-4">
  39. <form method="POST" action="" >
  40. <div class="form-group">
  41. <label>姓名</label>
  42. <input class="form-control" type="text" name="name" value="<?=$info['u_name']?>">
  43. </div>
  44. <div class="form-group">
  45. <label>手机号</label>
  46. <input class="form-control" type="text" name="phone" value="<?=$info['u_phone']?>" readonly>
  47. </div>
  48. <div class="form-group">
  49. <label>密码</label>
  50. <input class="form-control" placeholder="不填为不修改" type="password" name="pwd" value="">
  51. </div>
  52. <div class="form-group">
  53. <label>年龄</label>
  54. <input class="form-control" type="number" name="age" value="<?=$info['u_age']?>">
  55. </div>
  56. <div class="form-group">
  57. <div class="custom-control custom-switch">
  58. <input type="checkbox" class="custom-control-input" name="status" <?=$info['u_status']==1?'checked':''?> id="status">
  59. <label class="custom-control-label" for="status">状态</label>
  60. </div>
  61. </div>
  62. <hr class="my-4">
  63. <button type="submit" name="submit" class="btn btn-primary">提交修改</button>
  64. </form>
  65. </div>
  66. </body>
  67. </html>

6.运行效果

  • 列表页:
  • 登录页:
  • 编辑页:
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post