PHP案例:学⽣信息管理系统-- 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>
php用户管理系统下载<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;
}
?>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论