PHP实现简单的学生信息管理系统(web版)

时间:2022-05-27 08:21:36

(∩_∩)

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

 <!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
<script>
function doDel(id) {
if (confirm("确定要删除么?")) {
window.location = 'action.php?action=del&id='+id;
}
}
</script>
</head>
<body>
<center>
<?php
include_once "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=test;", "root", "");
} catch (PDOException $e) {
die("数据库连接失败" . $e->getMessage());
}
//2.解决中文乱码问题
$pdo->query("SET NAMES 'UTF8'");
//3.执行sql语句,并实现解析和遍历
$sql = "SELECT * FROM stu ";
foreach ($pdo->query($sql) as $row) {
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td>{$row['classid']}</td>";
echo "<td>
<a href='javascript:doDel({$row['id']})'>删除</a>
<a href='edit.php?id=({$row['id']})'>修改</a>
</td>";
echo "</tr>";
} ?> </table>
</center> </body>
</html>

index

menu.php

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

menu

add.php

 <html>
<head>
<title>学生信息管理</title>
</head>
<body>
<center>
<?php include_once "menu.php"; ?>
<h3>增加学生信息</h3> <form id="addstu" name="addstu" method="post" action="action.php?action=add">
<table>
<tr>
<td>姓名</td>
<td><input id="name" name="name" type="text"/></td> </tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="m"/>&nbsp;男
<input type="radio" name="sex" value="w"/>&nbsp;女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" id="age"/></td>
</tr>
<tr>
<td>班级</td>
<td><input id="classid" name="classid" type="text"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="增加"/>&nbsp;&nbsp;
<input type="reset" value="重置"/>
</td>
</tr>
</table> </form>
</center>
</body>
</html>

add

edit.php

 <html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title> </head>
<body>
<center>
<?php
include_once"menu.php";
//1.连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}
//2.防止中文乱码
$pdo->query("SET NAMES 'UTF8'");
//3.拼接sql语句,取出信息
$sql = "SELECT * FROM stu WHERE id =".$_GET['id'];
$stmt = $pdo->query($sql);//返回预处理对象
if($stmt->rowCount()>0){
$stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
}else{
die("没有要修改的数据!");
}
?>
<form id="addstu" name="editstu" method="post" action="action.php?action=edit">
<input type="hidden" name="id" id="id" value="<?php echo $stu['id'];?>"/>
<table>
<tr>
<td>姓名</td>
<td><input id="name" name="name" type="text" value="<?php echo $stu['name']?>"/></td> </tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="m" <?php echo ($stu['sex']=="m")? "checked" : ""?>/>&nbsp;男
<input type="radio" name="sex" value="w" <?php echo ($stu['sex']=="w")? "checked" : ""?>/>&nbsp;女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" id="age" value="<?php echo $stu['age']?>"/></td>
</tr>
<tr>
<td>班级</td>
<td><input id="classid" name="classid" type="text" value="<?php echo $stu['classid']?>"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="修改"/>&nbsp;&nbsp;
<input type="reset" value="重置"/>
</td>
</tr>
</table> </form> </center>
</body>
</html>

edit

action.php

 <?php
//1.连接数据库
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;", "root", ""); } catch (PDOException $e) {
die("数据库连接失败" . $e->getMessage());
}
//2.防止中文乱码
$pdo->query("SET NAMES 'UTF8'");
//3.通过action的值进行对应操作
switch ($_GET['action']) {
case 'add':{ //增加操作
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid']; //写sql语句
$sql = "INSERT INTO stu VALUES (NULL ,'{$name}','{$sex}','{$age}','{$classid}')";
$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": { //1.获取表单信息
$id = $_GET['id'];
$sql = "DELETE FROM stu WHERE id={$id}";
$pdo->exec($sql);
header("Location:index.php");//跳转到首页
break;
}
case "edit" :{ //1.获取表单信息
$id = $_POST['id'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$classid = $_POST['classid'];
$age = $_POST['age']; $sql = "UPDATE stu SET name='{$name}',sex='{$sex}',age='{$age}',classid='{$classid}' 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;
} }

action

3.效果

PHP实现简单的学生信息管理系统(web版)

PHP实现简单的学生信息管理系统(web版)PHP实现简单的学生信息管理系统(web版)PHP实现简单的学生信息管理系统(web版)

4.总结

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

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

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