代码一(查改+结合):
<!doctype html>
<html>
<head>
<meta charset="gbk">
<title>学生信息myschool</title>
</head>
<body >
<h1 align="center">学生信息表</h1>
<form action="" method="post" name="indexf">
<p align="center"><input type="button" value="新增" name="inbut" onClick="location.href='insert.php'"></p>
<p align="center"><input type="text" name="sel"><input type="submit" name="selsub" value="搜索"></p>
<table align="center" border="1px" cellspacing="0px" width="800px">
<tr><th>学号</th><th>姓名</th><th>性别</th><th>年龄</th><th>操作</th></tr>
<?php
session_start();//开启缓存区
$link=mysqli_connect('localhost','root','root','myschool');
if(!$link){
exit('数据库连接失败!');
}//确定是否连接成功
if(empty($_POST["selsub"])){
$res=mysqli_query($link,"select * from students order by stuid");
}
else{
$sel=$_POST["sel"];
$res=mysqli_query($link,"select * from students where stuid like '%$sel%' or stuname like '%$sel%' or stusex like '%$sel%' or age like '%$sel%'");//结果集
}
while($row=mysqli_fetch_array($res)){ //取一行值
echo'<tr align="center" >';
echo"<td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td>
<td>
<input type='submit' name='upsub$row[0]' value='修改'/>
<input type='submit' name='delsub$row[0]' value='删除'/>
</td>";
echo'</tr>';
//修改按钮实现代码:
if(!empty($_POST["upsub$row[0]"])){
echo'<tr align="center">';
echo"<td>$row[0]</td>
<td><input type='text' name='upsn' value='姓名'/></td>
<td><input type='text' name='upss' value='性别'/></td>
<td><input type='text' name='upsa' value='年龄'/></td>
<td><input type='submit' name='upsubs$row[0]' value='完成修改'/></td>";
echo'</tr>';
}
if(!empty($_POST["upsubs$row[0]"])){
$upsn=$_POST["upsn"];
$upss=$_POST["upss"];
$upsa=$_POST["upsa"];
mysqli_query($link,"update students set stuname='$upsn',stusex='$upss',age='$upsa' where stuid=$row[0]");
header('location:#');
}
//删除按钮实现代码:
if(!empty($_POST["delsub$row[0]"])){
$_SESSION['del']=$row[0];//将数组放到缓存区
echo'<script>
if(confirm("是否删除?") == true){
location.href="delst.php";
}
</script>';//使用js设置弹窗
}
}
mysqli_close($link);
?>
</table>
</form>
</body>
</html>
代码二(增):
<!doctype html>
<html>
<head>
<meta charset="gbk">
<title>新增学生记录</title>
</head>
<body>
<h1 align="center">新增学生记录</h1>
<form action="" method="post" name="addindex">
<p align="center">学生姓名:<input type="text" name="sn"></p>
<p align="center">学生性别:<input type="text" name="ss"></p>
<p align="center">学生年龄:<input type="text" name="sa"></p>
<p align="center"><input type="submit" name="addsub" value="提交"></p>
</form>
<?php
$link=mysqli_connect('localhost','root','root','myschool');
if(!$link){
exit('数据库连接失败!');
}//确定是否连接成功
if(!empty($_POST["addsub"])){
$sn=$_POST["sn"];
$ss=$_POST["ss"];
$sa=$_POST["sa"];
mysqli_query($link,"insert students(stuname,stusex,age) values ('$sn','$ss','$sa')");
header('location:myschool.php');
}
mysqli_close($link);
?>
</body>
</html>
代码三(删):
<!doctype html>
<html>
<head>
<meta charset="gbk">
<title>删除记录</title>
</head>
<body>
<?php
$link=mysqli_connect('localhost','root','root','myschool');
if(!$link){
exit('数据库连接失败!');
}//确定是否连接成功
session_start();//开启缓存
$del=$_SESSION['del'];
mysqli_query($link,"delete from students where stuid=$del");
unset($_SESSION['del']);
header('location:myschool.php');
mysqli_close($link);
?>
</body>
</html>
结果图(每个按钮都能使用):
DBMS:
知识来源:https://www.bilibili.com/video/BV1CV411s7im?share_source=copy_web