您的位置:首页 > 编程语言 > PHP开发

PHP案例:学生信息管理系统

2017-05-24 11:47 453 查看
-- Database:
test


-- 表的结构
message


CREATE TABLE `message` (
`id` tinyint(1) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`sex` varchar(50) NOT NULL,
`age` tinyint(1) NOT NULL,
`classid` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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 message ";
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>

menu.php

<!DOCTYPE html>
<html lang="en">
<body>
<h2>学生管理系统</h2>
<a href="index.php"> 浏览学生</a>
<a href="add.php"> 添加学生</a>
<hr>
</body>
</html>

add.php

<html>
<head>
<title>学生信息管理</title>
</head>
<body>
<center>
<?php include("menu.php"); ?>
<h3>增加学生信息</h3>
<form method="post" action="action.php?action=add">

<table>
<tr>
<td>姓名</td>
<td><input  name="name" type="text"/></td>

</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="男"/> 男
<input type="radio" name="sex" value="女"/> 女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td>班级</td>
<td><input  name="classid" type="text"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="增加"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>

</form>
</center>
</body>
</html>






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 message WHERE id =" . $_GET['id'];
$stmt = $pdo->query($sql);//返回预处理对象
if ($stmt->rowCount() > 0) {
$stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
} else {
die("没有要修改的数据!");
}
?>
<form 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="男" <?php echo ($stu['sex'] == "男") ? "checked" : "" ?>/> 男
<input type="radio" name="sex" value="女" <?php echo ($stu['sex'] == "女") ? "checked" : "" ?>/> 女
</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> </td>
<td><input type="submit" value="修改"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>

</form>

</center>
</body>
</html>

action.php

<?php

//1.连接数据库
try{
$pdo = new PDO("mysql:host=localhost;dbname=test;","root","");
}catch(PDOException $e){
die("数据库连接失败".$e->getMessage());
}

//2.通过action的值做地应操作

switch($_GET['action']){
case "add"://增加操作
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid'];

$sql = "insert into message 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"; //删除操作
$id = $_GET['id'];
$sql = "delete from message where id={$id}";
$pdo->exec($sql);
header("Location:index.php");
break;

case "edit":

//1.获取表单信息
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$classid = $_POST['classid'];
$id = $_POST['id'];

$sql = "update message 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;
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: