当前位置:首页 > 综合实例 > 列表

PDO实现学生管理系统

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-21 09:49:31 浏览: 评论:0 

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

需要建立如下文件:

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.    
  31.      }catch(PDOException $e){ 
  32.       die("fail to connect db".$e->getMessage()); 
  33.      } 
  34.      //2. 执行数据库,并解析遍历 
  35.      $sql = "SELECT * FROM users"; 
  36.      foreach($pdo->query($sql) as $val){ 
  37.       echo "<tr>"; 
  38.       echo "<td>{$val['id']}</td>"; 
  39.       echo "<td>{$val['name']}</td>"; 
  40.       echo "<td>{$val['sex']}</td>"; 
  41.       echo "<td>{$val['age']}</td>"; 
  42.       echo "<td>{$val['class']}</td>"; 
  43.       echo "<td> 
  44.          <a href='javascript:doDel({$val['id']})'>删除</a> 
  45.          <a href='edit.php?id={$val['id']}'>修改</a> 
  46.         </td>"; 
  47.       echo "</tr>"; 
  48.      } 
  49.     ?> 
  50.    </table> 
  51.    
  52.   </center> 
  53.  </body> 
  54. </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.    
  17.    <tr> 
  18.     <td>姓别</td> 
  19.     <td> 
  20.      <input type="radio" name="sex" value="m"/>男 
  21.      <input type="radio" name="sex" value="w"/>女 
  22.     </td> 
  23.    
  24.    </tr> 
  25.    
  26.    <tr> 
  27.     <td>年龄</td> 
  28.     <td><input type="text" name="age"/></td> 
  29.    </tr> 
  30.    
  31.    <tr> 
  32.     <td>班级</td> 
  33.     <td><input type="text" name="class"/></td> 
  34.    </tr> 
  35.    
  36.    <tr> 
  37.     <td> </td> 
  38.     <td> 
  39.      <input type="submit" value="增加"/> 
  40.      <input type="submit" value="重置"/> 
  41.     </td> 
  42.    
  43.    </tr> 
  44.   </table> 
  45.  </form> 
  46. </center> 
  47. </body> 
  48. </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.    
  14.  }catch(PDOException $e){ 
  15.   die("fail to connect db".$e->getMessage()); 
  16.  } 
  17.  //2. 拼装sql语句,取出信息 
  18.  $sql = "SELECT * FROM users WHERE id=".$_GET['id']; 
  19.  $stmt = $pdo->query($sql); 
  20.  if($stmt->rowCount() > 0){ 
  21.   $stu = $stmt->fetch(PDO::FETCH_ASSOC); //解析数据 
  22.  }else{ 
  23.   die("没有修改的信息"); 
  24.  } 
  25.  ?> 
  26.  <h3>修改学生信息</h3> 
  27.  <form action="action.php?action=edit" method="post"> 
  28.  <!-- 以隐藏域的方式添加id  --> 
  29.   <input type="hidden" name="id" value="<?php echo $stu['id']; ?>"> 
  30.   <table> 
  31.    <tr> 
  32.     <td>姓名</td> 
  33.     <td><input type="text" name="name" value="<?php echo $stu['name'];?>"/></td> 
  34.    </tr> 
  35.    
  36.    <tr> 
  37.     <td>姓别</td> 
  38.     <td> 
  39.      <input type="radio" name="sex" value="m" <?php echo ($stu['sex']== 
  40.       "m")? "checked": ""; ?>/>男 
  41.      <input type="radio" name="sex" value="w" <?php echo ($stu['sex']== 
  42.       "w")? "checked": ""; ?>/>女 
  43.     </td> 
  44.    
  45.    </tr> 
  46.    
  47.    <tr> 
  48.     <td>年龄</td> 
  49.     <td><input type="text" name="age" value="<?php echo $stu['age'];?>"/></td> 
  50.    </tr> 
  51.    
  52.    <tr> 
  53.     <td>班级</td> 
  54.     <td><input type="text" name="class" value="<?php echo $stu['class'];?>"/></td> 
  55.    </tr> 
  56.    
  57.    <tr> 
  58.     <td> </td> 
  59.     <td> 
  60.      <input type="submit" value="修改"/> 
  61.      <input type="submit" value="重置"/> 
  62.     </td> 
  63.    
  64.    </tr> 
  65.   </table> 
  66.  </form> 
  67. </center> 
  68. </body> 
  69. </html> 

action.php

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

Tags: PDO学生管理系统

分享到: