题目
代码
index.php
<html>
<head>
<title>学生管理信息</title>
</head>
<style>
.display{
display: inline-block;
}
</style>
<body>
<div class="display">
<h3>学生信息浏览 <a href='stuInsert.php'>新增</a></h3>
<table width="600" border="1">
<tr>
<td>ID</td>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>操作</td>
</tr>
<?php
//连接数据库
try {
$conn=new mysqli("localhost","root","","work");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
//执行SQL语句
$sql='select * from student';
foreach ($conn->query($sql) as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['num']}</td>";
echo "<td>{$row['name']}</td>";
if($row['sex']=='m'){
echo "<td>男</td>";
}else if($row['sex']=='w'){
echo "<td>女</td>";
}
echo "<td>{$row['age']}</td>";
$tab="stu";
echo "<td><a href='javascript:stuDel({$row['id']})'>删除</a> | <a href='stuUpdate.php?id={$row['id']}'>修改</a></td>";
echo "</tr>";
}
?>
</table>
</div>
<div class="display">
<h3>课程信息浏览 <a href='classInsert.php'>新增</a></h3>
<table width="600" border="1">
<tr>
<td>ID</td>
<td>课程编号</td>
<td>课程名</td>
<td>任课老师</td>
<td>上课时间</td>
<td>上课地点</td>
</tr>
<?php
//连接数据库
try {
$conn=new mysqli("localhost","root","","work");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
//执行SQL语句
$sql='select * from class';
foreach ($conn->query($sql) as $row){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['num']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['teacher']}</td>";
echo "<td>{$row['time']}</td>";
echo "<td>{$row['position']}</td>";
echo "<td><a href='javascript:classDel({$row['id']})'>删除</a> | <a href='classUpdate.php?id={$row['id']}'>修改</a></td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
<script type="text/javascript">
function stuDel(id){
if(confirm('是否确定删除?')) {
window.location = 'action.php?action=stuDel&id=' + id;
}
}
function classDel(id){
if(confirm('是否确定删除?')) {
window.location = 'action.php?action=classDel&id=' + id;
}
}
</script>
</html>
action.php
<?php
try {
$conn=new mysqli("localhost","root","","work");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
//操作
switch ($_GET['action']){
case "stuAdd":
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$num=$_POST['num'];
$sql="insert into student values(null,'{$num}','{$name}','{$sex}','{$age}')";
$rw=$conn->query($sql);
if($rw>0){
echo "<script>alert('增加成功');window.location='index.php'</script>";
}else{
echo "<script>alert('增加失败');window.history.back()</script>";
}
break;
case "stuDel":
$id=$_GET['id'];
$sql="delete from student where id={$id}";
$conn->query($sql);
header("Location:index.php");
break;
case "stuEdit":
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$num=$_POST['num'];
$id=$_POST['id'];
$sql="update student set name='{$name}',sex='{$sex}',age='{$age}',num='{$num}' where id={$id}";
$rw=$conn->query($sql);
if($rw>0){
echo "<script>alert('修改成功');window.location='index.php'</script>";
}else{
echo "<script>alert('修改失败');window.history.back()</script>";
}
break;
case "classAdd":
$name=$_POST['name'];
$teacher=$_POST['teacher'];
$time=$_POST['time'];
$num=$_POST['num'];
$position=$_POST['position'];
$sql="insert into class values(null,'{$num}','{$name}','{$teacher}','{$time}','{$position}')";
$rw=$conn->query($sql);
if($rw>0){
echo "<script>alert('增加成功');window.location='index.php'</script>";
}else{
echo "<script>alert('增加失败');window.history.back()</script>";
}
break;
case "classDel":
$id=$_GET['id'];
$sql="delete from class where id={$id}";
$conn->query($sql);
header("Location:index.php");
break;
case "classEdit":
$name=$_POST['name'];
$teacher=$_POST['teacher'];
$time=$_POST['time'];
$num=$_POST['num'];
$position=$_POST['position'];
$id=$_POST['id'];
$sql="update class set num='{$num}',name ='{$name}',teacher='{$teacher}',time ='{$time}',position='{$position}' where id={$id}";
$rw=$conn->query($sql);
if($rw>0){
echo "<script>alert('修改成功');window.location='index.php'</script>";
}else{
echo "<script>alert('修改失败');window.history.back()</script>";
}
break;
}
stuInsert.php
<html>
<head>
<title>学生管理信息</title>
</head>
<body>
<center>
<h3>增加学生</h3>
<form action="action.php?action=stuAdd" method="post">
<table>
<tr>
<td>学号</td>
<td><input type="text" name="num" /></td>
</tr>
<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>
<input type="submit" value="增加" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
stuUpdate.php
<html>
<head>
<title>学生管理信息</title>
</head>
<body>
<center>
<?php
try {
$conn=new mysqli("localhost","root","","work");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
$sql="select * from student where id=".$_GET['id'];
$stmt=$conn->query($sql);
if($stmt->num_rows >0){
$stu=$stmt->fetch_assoc();
}else{
die("没有修改");
}
?>
<h3>修改学生</h3>
<form action="action.php?action=stuEdit" method="post">
<table>
<tr>
<td>学号</td>
<td><input type="text" name="num" value="<?php echo $stu['num']?>"/></td>
</tr>
<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>
<input type="hidden" name="id" value="<?php echo $stu['id']?>"/>
<tr>
<td>
<input type="submit" value="修改" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
classInsert.php
<html>
<head>
<title>学生管理信息</title>
</head>
<body>
<center>
<h3>增加课程信息</h3>
<form action="action.php?action=classAdd" method="post">
<table>
<tr>
<td>课程编号</td>
<td><input type="text" name="num" /></td>
</tr>
<tr>
<td>课程名</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>任课老师</td>
<td>
<td><input type="text" name="teacher" /></td>
</td>
</tr>
<tr>
<td>上课时间</td>
<td><input type="text" name="time" /></td>
</tr>
<tr>
<td>上课地点</td>
<td><input type="text" name="position" /></td>
</tr>
<tr>
<td>
<input type="submit" value="增加" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
classUpdate.php
<html>
<head>
<title>学生管理信息</title>
</head>
<body>
<center>
<?php
try {
$conn=new mysqli("localhost","root","","work");
} catch (Exception $e) {
die("数据库连接失败".$e->getMessage());
}
$sql="select * from class where id=".$_GET['id'];
$stmt=$conn->query($sql);
if($stmt->num_rows >0){
$class=$stmt->fetch_assoc();
}else{
die("没有修改");
}
?>
<h3>修改课程</h3>
<form action="action.php?action=classEdit" method="post">
<table>
<tr>
<td>课程编号</td>
<td><input type="text" name="num" value="<?php echo $class['num']?>"/></td>
</tr>
<tr>
<td>课程名</td>
<td><input type="text" name="name" value="<?php echo $class['name']?>"/></td>
</tr>
<tr>
<td>任课老师</td>
<td><input type="text" name="teacher" value="<?php echo $class['teacher']?>"/></td>
</tr>
<tr>
<td>上课时间</td>
<td><input type="text" name="time" value="<?php echo $class['time']?>"/></td>
</tr>
<tr>
<td>上课地点</td>
<td><input type="text" name="position" value="<?php echo $class['position']?>"/></td>
</tr>
<input type="hidden" name="id" value="<?php echo $class['id']?>"/>
<tr>
<td>
<input type="submit" value="修改" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
效果
统计界面
增加界面
修改界面