您现在的位置是:群英 > 开发技术 > PHP语言
如何用PDO实现一个学生管理系统,代码怎么写
Admin发表于 2022-05-12 11:25:47726 次浏览
这篇文章给大家介绍了“如何用PDO实现一个学生管理系统,代码怎么写”的相关知识,讲解详细,步骤过程清晰,对大家进一步学习和理解“如何用PDO实现一个学生管理系统,代码怎么写”有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。


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

需要建立如下文件:

  • 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;
}

以上就是关于“如何用PDO实现一个学生管理系统,代码怎么写”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: 学生管理系统
相关信息推荐
2022-01-04 18:54:21 
摘要:这篇文章给大家分享的是C++中智能指针的相关内容,主要介绍了aoto_ptr、unique_ptr、share_ptr和weak_ptr这四种智能指针的使用,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
2021-11-02 17:17:48 
摘要:这篇文章给大家分享的是PHP中强制类型转换的内容。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,对大家学习和了解PHP强制类型转换的方式有一定的帮,感兴趣的朋友接下来一起跟随小编看看吧。
2022-05-13 17:49:17 
摘要:本篇文章带大家聊聊Angular中的响应式表单,通过实例来介绍一下简单的表单实现方法,希望对大家有所帮助!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部