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

需要建立如下文件:

  • index.php
  • menu.php //菜单栏
  • add.php  //添加数据
  • edit.php // 编辑数据
  • action.php // 添加,删除,编辑的实现

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

menu.php:

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

index.php

<html>
 <head>
  <meta charset="utf-8">
  <title>学生信息管理系统</title>
 </head>
 <script>
  function dodel(id){
   if(confirm("是否要删除")){
    window.location='action.php?action=del&id='+id;
   }
  }
 </script>
 <body>
  <center>
   <?php include("menu.php");?>
   <h3>浏览学生信息</h3>
   <table width="600" border="1">
    <tr>
     <th>id</th>
     <th>姓名</th>
     <th>姓别</th>
     <th>年龄</th>
     <th>班级</th>
     <th>操作</th>
    </tr>
    <?php
     //1. 连接数据库
     try{
      $pdo = new pdo("mysql:host=localhost;dbname=myapp;", "root", "");
 
     }catch(pdoexception $e){
      die("fail to connect db".$e->getmessage());
     }
     //2. 执行数据库,并解析遍历
     $sql = "select * from users";
     foreach($pdo->query($sql) as $val){
      echo "<tr>";
      echo "<td>{$val['id']}</td>";
      echo "<td>{$val['name']}</td>";
      echo "<td>{$val['sex']}</td>";
      echo "<td>{$val['age']}</td>";
      echo "<td>{$val['class']}</td>";
      echo "<td>
         <a href='javascript:dodel({$val['id']})'>删除</a>
         <a href='edit.php?id={$val['id']}'>修改</a>
        </td>";
      echo "</tr>";
     }
    ?>
   </table>
 
  </center>
 </body>
</html>

add.php

<html>
<head>
 <meta charset="utf-8">
 <title>学生信息管理系统</title>
</head>
<body>
<center>
 <?php include("menu.php");?>
 <h3>增加学生信息</h3>
 <form action="action.php?action=add" method="post">
  <table>
   <tr>
    <td>姓名</td>
    <td><input type="text" name="name"/></td>
   </tr>
 
   <tr>
    <td>姓别</td>
    <td>
     <input type="radio" name="sex" value="m"/>男
     <input type="radio" name="sex" value="w"/>女
    </td>
 
   </tr>
 
   <tr>
    <td>年龄</td>
    <td><input type="text" name="age"/></td>
   </tr>
 
   <tr>
    <td>班级</td>
    <td><input type="text" name="class"/></td>
   </tr>
 
   <tr>
    <td> </td>
    <td>
     <input type="submit" value="增加"/>
     <input type="submit" value="重置"/>
    </td>
 
   </tr>
  </table>
 </form>
</center>
</body>
</html>

edit.php

<html>
<head>
 <meta charset="utf-8">
 <title>学生信息管理系统</title>
</head>
<body>
<center>
 <?php include("menu.php");
 //获取修改信息
 //1. 连接数据库
 try{
  $pdo = new pdo("mysql:host=localhost;dbname=myapp;", "root", "");
 
 }catch(pdoexception $e){
  die("fail to connect db".$e->getmessage());
 }
 //2. 拼装sql语句,取出信息
 $sql = "select * from users where id=".$_get['id'];
 $stmt = $pdo->query($sql);
 if($stmt->rowcount() > 0){
  $stu = $stmt->fetch(pdo::fetch_assoc); //解析数据
 }else{
  die("没有修改的信息");
 }
 ?>
 <h3>修改学生信息</h3>
 <form action="action.php?action=edit" method="post">
 <!-- 以隐藏域的方式添加id  -->
  <input type="hidden" name="id" value="<?php echo $stu['id']; ?>">
  <table>
   <tr>
    <td>姓名</td>
    <td><input type="text" name="name" value="<?php echo $stu['name'];?>"/></td>
   </tr>
 
   <tr>
    <td>姓别</td>
    <td>
     <input type="radio" name="sex" value="m" <?php echo ($stu['sex']==
      "m")? "checked": ""; ?>/>男
     <input type="radio" name="sex" value="w" <?php echo ($stu['sex']==
      "w")? "checked": ""; ?>/>女
    </td>
 
   </tr>
 
   <tr>
    <td>年龄</td>
    <td><input type="text" name="age" value="<?php echo $stu['age'];?>"/></td>
   </tr>
 
   <tr>
    <td>班级</td>
    <td><input type="text" name="class" value="<?php echo $stu['class'];?>"/></td>
   </tr>
 
   <tr>
    <td> </td>
    <td>
     <input type="submit" value="修改"/>
     <input type="submit" value="重置"/>
    </td>
 
   </tr>
  </table>
 </form>
</center>
</body>
</html>

action.php

<?php
//1. 连接数据库
try{
 $pdo = new pdo("mysql:host=localhost;dbname=myapp;", "root", "");
 
}catch(pdoexception $e){
 die("fail to connect db".$e->getmessage());
}
//2. 通过action的值做相应的操作
switch($_get['action']){
 case "add": //增加操作
  $name = $_post['name'];
  $sex = $_post['sex'];
  $age = $_post['age'];
  $class = $_post['class'];
 
  $sql = "insert into users values (null, '{$name}','{$sex}', '{$age}', '{$class}')";
  $rw = $pdo->exec($sql);
  if($rw > 0){
   echo "<script>alert('增加成功'); window.location='index.php'</script>";
  }else{
   echo "<script>alert('增加失败'); window.history.back()</script>";
  }
  break;
 case "del":
  $id = $_get['id'];
  $sql = "delete from users where id={$id}";
  $pdo->exec($sql);
  header("location:index.php");
  break;
 case "edit":
  $name = $_post['name'];
  $sex = $_post['sex'];
  $age = $_post['age'];
  $class = $_post['class'];
  $id = $_post['id'];
 
  $sql = "update users set name='{$name}',sex='{$sex}',age={$age},class={$class}
    where id={$id}";
  $rw = $pdo->exec($sql);
  if($rw > 0){
   echo "<script>alert('修改成功'); window.location='index.php'</script>";
  }else{
   echo "<script>alert('修改失败'); window.history.back()</script>";
  }
  break;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。