zoukankan      html  css  js  c++  java
  • PHP实现简单的学生信息管理系统(web版)

    (∩_∩)

    1.概述

    学了php的一些基础,包括HTML,php,pdo,mysql操作等,一直都没有将它们有机结合。最近写了一个简单的网页版学生信息管理系统,前台用HTML,脚本用到了JavaScript和PHP,数据库用到了MySQL。麻雀虽小,五脏俱全。算是对这些知识的一次总结吧。

    2.源码

    工程包括5个php文件:

    index.php,负责首页展现;

    menu.php,负责前台的菜单实现;

    add.php,负责添加页面的实现;

    edit.php,负责编辑页面的实现;

    action.php,负责对增删查改操作的实现。

    index.php

     1 <!DOCTYPE html>
     2 <head>
     3     <meta charset="UTF-8">
     4     <title>学生信息管理</title>
     5     <script>
     6         function doDel(id) {
     7             if (confirm("确定要删除么?")) {
     8                 window.location = 'action.php?action=del&id='+id;
     9             }
    10         }
    11     </script>
    12 </head>
    13 <body>
    14 <center>
    15     <?php
    16     include_once "menu.php";
    17     ?>
    18     <h3>浏览学生信息</h3>
    19     <table width="600" border="1">
    20         <tr>
    21             <th>ID</th>
    22             <th>姓名</th>
    23             <th>性别</th>
    24             <th>年龄</th>
    25             <th>班级</th>
    26             <th>操作</th>
    27         </tr>
    28         <?php
    29         //1.连接数据库
    30         try {
    31             $pdo = new PDO("mysql:host=localhost;dbname=test;", "root", "");
    32         } catch (PDOException $e) {
    33             die("数据库连接失败" . $e->getMessage());
    34         }
    35         //2.解决中文乱码问题
    36         $pdo->query("SET NAMES 'UTF8'");
    37         //3.执行sql语句,并实现解析和遍历
    38         $sql = "SELECT * FROM stu ";
    39         foreach ($pdo->query($sql) as $row) {
    40             echo "<tr>";
    41             echo "<td>{$row['id']}</td>";
    42             echo "<td>{$row['name']}</td>";
    43             echo "<td>{$row['sex']}</td>";
    44             echo "<td>{$row['age']}</td>";
    45             echo "<td>{$row['classid']}</td>";
    46             echo "<td>
    47                     <a href='javascript:doDel({$row['id']})'>删除</a>
    48                     <a href='edit.php?id=({$row['id']})'>修改</a>
    49                   </td>";
    50             echo "</tr>";
    51         }
    52 
    53         ?>
    54 
    55     </table>
    56 </center>
    57 
    58 </body>
    59 </html>
    index

    menu.php

    1 <h2>学生信息管理</h2>
    2 <a href="index.php">浏览学生</a>
    3 <a href="add.php">增加学生</a>
    4 <hr>
    menu

    add.php

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

    edit.php

     1 <html>
     2 <head>
     3     <meta charset="UTF-8">
     4     <title>学生信息管理</title>
     5 
     6 </head>
     7 <body>
     8 <center>
     9     <?php
    10     include_once"menu.php";
    11     //1.连接数据库
    12     try{
    13         $pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
    14     }catch(PDOException $e){
    15         die("数据库连接失败".$e->getMessage());
    16     }
    17     //2.防止中文乱码
    18     $pdo->query("SET NAMES 'UTF8'");
    19     //3.拼接sql语句,取出信息
    20     $sql = "SELECT * FROM stu WHERE id =".$_GET['id'];
    21     $stmt = $pdo->query($sql);//返回预处理对象
    22     if($stmt->rowCount()>0){
    23         $stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
    24     }else{
    25         die("没有要修改的数据!");
    26     }
    27     ?>
    28     <form id="addstu" name="editstu" method="post" action="action.php?action=edit">
    29         <input type="hidden" name="id" id="id" value="<?php echo $stu['id'];?>"/>
    30         <table>
    31             <tr>
    32                 <td>姓名</td>
    33                 <td><input id="name" name="name" type="text" value="<?php echo $stu['name']?>"/></td>
    34 
    35             </tr>
    36             <tr>
    37                 <td>性别</td>
    38                 <td><input type="radio" name="sex" value="m" <?php echo ($stu['sex']=="m")? "checked" : ""?>/>&nbsp;男
    39                     <input type="radio" name="sex" value="w"  <?php echo ($stu['sex']=="w")? "checked" : ""?>/>&nbsp;女
    40                 </td>
    41             </tr>
    42             <tr>
    43                 <td>年龄</td>
    44                 <td><input type="text" name="age" id="age" value="<?php echo $stu['age']?>"/></td>
    45             </tr>
    46             <tr>
    47                 <td>班级</td>
    48                 <td><input id="classid" name="classid" type="text" value="<?php echo $stu['classid']?>"/></td>
    49             </tr>
    50             <tr>
    51                 <td>&nbsp;</td>
    52                 <td><input type="submit" value="修改"/>&nbsp;&nbsp;
    53                     <input type="reset" value="重置"/>
    54                 </td>
    55             </tr>
    56         </table>
    57 
    58     </form>
    59 
    60 
    61 
    62 </center>
    63 </body>
    64 </html>
    edit

    action.php

     1 <?php
     2 //1.连接数据库
     3 try {
     4     $pdo = new PDO("mysql:host=localhost;dbname=test;", "root", "");
     5 
     6 } catch (PDOException $e) {
     7     die("数据库连接失败" . $e->getMessage());
     8 }
     9 //2.防止中文乱码
    10 $pdo->query("SET NAMES 'UTF8'");
    11 //3.通过action的值进行对应操作
    12 switch ($_GET['action']) {
    13     case 'add':{   //增加操作
    14         $name = $_POST['name'];
    15         $sex = $_POST['sex'];
    16         $age = $_POST['age'];
    17         $classid = $_POST['classid'];
    18 
    19         //写sql语句
    20         $sql = "INSERT INTO stu VALUES (NULL ,'{$name}','{$sex}','{$age}','{$classid}')";
    21         $rw = $pdo->exec($sql);
    22         if ($rw > 0) {
    23             echo "<script> alert('增加成功');
    24                             window.location='index.php'; //跳转到首页
    25                  </script>";
    26         } else {
    27             echo "<script> alert('增加失败');
    28                             window.history.back(); //返回上一页
    29                  </script>";
    30         }
    31         break;
    32     }
    33     case "del": {    //1.获取表单信息
    34         $id = $_GET['id'];
    35         $sql = "DELETE FROM stu WHERE id={$id}";
    36         $pdo->exec($sql);
    37         header("Location:index.php");//跳转到首页
    38         break;
    39     }
    40     case "edit" :{   //1.获取表单信息
    41         $id = $_POST['id'];
    42         $name = $_POST['name'];
    43         $sex = $_POST['sex'];
    44         $classid = $_POST['classid'];
    45         $age = $_POST['age'];
    46 
    47         $sql = "UPDATE stu SET name='{$name}',sex='{$sex}',age='{$age}',classid='{$classid}' WHERE id='{$id}'";
    48         $rw=$pdo->exec($sql);
    49         if($rw>0){
    50             echo "<script>alert('修改成功');window.location='index.php'</script>";
    51         }else{
    52             echo "<script>alert('修改失败');window.history.back()</script>";
    53         }
    54 
    55 
    56         break;
    57     }
    58 
    59 }
    action

    3.效果

    4.总结

    (1)在做这个小工程时,感觉自己有了很大提高。想到从前台的改变,直接对数据库实现了操作,很神奇。发现自己的弱点是数据传输,利用参数传递比较薄弱。对于JavaScript脚本的使用还很生硬。

    (2)在写的过程中遇到了编码的问题,在这里说明对于服务器的字符集,数据库的字符集,每张表的字符集,页面的字符集都要一致,最好是都设置成UTF-8,然后为了解决从数据库取出数据时的中文乱码问题,在php中连接数据库后就需要执行一句:“SET NAMES 'UTF8'”。

    由于我用的xampp,需要在创建表时,选择general_utf8_ci的字符集。

  • 相关阅读:
    mongodb浅析
    java8学习的一点总结
    mysql 存储引擎对索引的支持
    Ubuntu16.04下安装OpenCV2.4.13
    windows配置远程桌面连接到ubuntu
    Ubuntu16.04调整屏幕分辨率至1920*1080
    Ubuntu 16.04 安装NodeJs
    Ubuntu安装SSH服务
    基于MNIST数据集使用TensorFlow训练一个包含一个隐含层的全连接神经网络
    基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
  • 原文地址:https://www.cnblogs.com/yuxiuyan/p/5754717.html
Copyright © 2011-2022 走看看