A-A+

PDO实现学生管理系统

2022年02月21日 我爱编程 暂无评论

这里实现一个简单的学生管理系统,供大家参考,具体内容如下

需要建立如下文件:

index.php

menu.php //菜单栏

add.php  //添加数据

edit.php // 编辑数据

action.php // 添加,删除,编辑的实现

分别写一下每个文件的代码:

menu.php:

  1. <html>
  2. <h2>学生信息管理</h2>
  3. <a href="index.php" rel="external nofollow" >浏览学生</a>
  4. <a href="add.php" rel="external nofollow" >增加学生</a>
  5. <hr>
  6. </html>

index.php

  1. <html>
  2.  <head>
  3.   <meta charset="UTF-8">
  4.   <title>学生信息管理系统</title>
  5.  </head>
  6.  <script>
  7.   function doDel(id){
  8.    if(confirm("是否要删除")){
  9.     window.location='action.php?action=del&id='+id;
  10.    }
  11.   }
  12.  </script>
  13.  <body>
  14.   <center>
  15.    <?php include("menu.php");?>
  16.    <h3>浏览学生信息</h3>
  17.    <table width="600" border="1">
  18.     <tr>
  19.      <th>ID</th>
  20.      <th>姓名</th>
  21.      <th>姓别</th>
  22.      <th>年龄</th>
  23.      <th>班级</th>
  24.      <th>操作</th>
  25.     </tr>
  26.     <?php
  27.      //1. 连接数据库
  28.      try{
  29.       $pdo = new PDO("mysql:host=localhost;dbname=myapp;""root""");
  30.      }catch(PDOException $e){
  31.       die("fail to connect db".$e->getMessage());
  32.      }
  33.      //2. 执行数据库,并解析遍历
  34.      $sql = "SELECT * FROM users";
  35.      foreach($pdo->query($sqlas $val){
  36.       echo "<tr>";
  37.       echo "<td>{$val['id']}</td>";
  38.       echo "<td>{$val['name']}</td>";
  39.       echo "<td>{$val['sex']}</td>";
  40.       echo "<td>{$val['age']}</td>";
  41.       echo "<td>{$val['class']}</td>";
  42.       echo "<td>
  43.          <a href='javascript:doDel({$val['id']})'>删除</a>
  44.          <a href='edit.php?id={$val['id']}'>修改</a>
  45.         </td>";
  46.       echo "</tr>";
  47.      }
  48.     ?>
  49.    </table>
  50.   </center>
  51.  </body>
  52. </html>

add.php

  1. <html>
  2. <head>
  3.  <meta charset="UTF-8">
  4.  <title>学生信息管理系统</title>
  5. </head>
  6. <body>
  7. <center>
  8.  <?php include("menu.php");?>
  9.  <h3>增加学生信息</h3>
  10.  <form action="action.php?action=add" method="post">
  11.   <table>
  12.    <tr>
  13.     <td>姓名</td>
  14.     <td><input type="text" name="name"/></td>
  15.    </tr>
  16.    <tr>
  17.     <td>姓别</td>
  18.     <td>
  19.      <input type="radio" name="sex" value="m"/>男
  20.      <input type="radio" name="sex" value="w"/>女
  21.     </td>
  22.    </tr>
  23.    <tr>
  24.     <td>年龄</td>
  25.     <td><input type="text" name="age"/></td>
  26.    </tr>
  27.    <tr>
  28.     <td>班级</td>
  29.     <td><input type="text" name="class"/></td>
  30.    </tr>
  31.    <tr>
  32.     <td> </td>
  33.     <td>
  34.      <input type="submit" value="增加"/>
  35.      <input type="submit" value="重置"/>
  36.     </td>
  37.    </tr>
  38.   </table>
  39.  </form>
  40. </center>
  41. </body>
  42. </html>

edit.php

  1. <html>
  2. <head>
  3.  <meta charset="UTF-8">
  4.  <title>学生信息管理系统</title>
  5. </head>
  6. <body>
  7. <center>
  8.  <?php include("menu.php");
  9.  //获取修改信息
  10.  //1. 连接数据库
  11.  try{
  12.   $pdo = new PDO("mysql:host=localhost;dbname=myapp;""root""");
  13.  }catch(PDOException $e){
  14.   die("fail to connect db".$e->getMessage());
  15.  }
  16.  //2. 拼装sql语句,取出信息
  17.  $sql = "SELECT * FROM users WHERE id=".$_GET['id'];
  18.  $stmt = $pdo->query($sql);
  19.  if($stmt->rowCount() > 0){
  20.   $stu = $stmt->fetch(PDO::FETCH_ASSOC); //解析数据
  21.  }else{
  22.   die("没有修改的信息");
  23.  }
  24.  ?>
  25.  <h3>修改学生信息</h3>
  26.  <form action="action.php?action=edit" method="post">
  27.  <!-- 以隐藏域的方式添加id  -->
  28.   <input type="hidden" name="id" value="<?php echo $stu['id']; ?>">
  29.   <table>
  30.    <tr>
  31.     <td>姓名</td>
  32.     <td><input type="text" name="name" value="<?php echo $stu['name'];?>"/></td>
  33.    </tr>
  34.    <tr>
  35.     <td>姓别</td>
  36.     <td>
  37.      <input type="radio" name="sex" value="m" <?php echo ($stu['sex']==
  38.       "m")? "checked"""; ?>/>男
  39.      <input type="radio" name="sex" value="w" <?php echo ($stu['sex']==
  40.       "w")? "checked"""; ?>/>女
  41.     </td>
  42.    </tr>
  43.    <tr>
  44.     <td>年龄</td>
  45.     <td><input type="text" name="age" value="<?php echo $stu['age'];?>"/></td>
  46.    </tr>
  47.    <tr>
  48.     <td>班级</td>
  49.     <td><input type="text" name="class" value="<?php echo $stu['class'];?>"/></td>
  50.    </tr>
  51.    <tr>
  52.     <td> </td>
  53.     <td>
  54.      <input type="submit" value="修改"/>
  55.      <input type="submit" value="重置"/>
  56.     </td>
  57.    </tr>
  58.   </table>
  59.  </form>
  60. </center>
  61. </body>
  62. </html>

action.php

  1. <?php
  2. //1. 连接数据库
  3. try{
  4.  $pdo = new PDO("mysql:host=localhost;dbname=myapp;""root""");
  5. }catch(PDOException $e){
  6.  die("fail to connect db".$e->getMessage());
  7. }
  8. //2. 通过action的值做相应的操作
  9. switch($_GET['action']){
  10.  case "add"//增加操作
  11.   $name = $_POST['name'];
  12.   $sex = $_POST['sex'];
  13.   $age = $_POST['age'];
  14.   $class = $_POST['class'];
  15.   $sql = "INSERT INTO users VALUES (null, '{$name}','{$sex}', '{$age}', '{$class}')";
  16.   $rw = $pdo->exec($sql);
  17.   if($rw > 0){
  18.    echo "<script>alert('增加成功'); window.location='index.php'</script>";
  19.   }else{
  20.    echo "<script>alert('增加失败'); window.history.back()</script>";
  21.   }
  22.   break;
  23.  case "del":
  24.   $id = $_GET['id'];
  25.   $sql = "DELETE FROM users WHERE id={$id}";
  26.   $pdo->exec($sql);
  27.   header("location:index.php");
  28.   break;
  29.  case "edit":
  30.   $name = $_POST['name'];
  31.   $sex = $_POST['sex'];
  32.   $age = $_POST['age'];
  33.   $class = $_POST['class'];
  34.   $id = $_POST['id'];
  35.   $sql = "UPDATE users SET name='{$name}',sex='{$sex}',age={$age},class={$class}
  36.     WHERE id={$id}";
  37.   $rw = $pdo->exec($sql);
  38.   if($rw > 0){
  39.    echo "<script>alert('修改成功'); window.location='index.php'</script>";
  40.   }else{
  41.    echo "<script>alert('修改失败'); window.history.back()</script>";
  42.   }
  43.   break;
  44. }

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录