<?php
session_start();
if(!isset($_SESSION['user'])){
header('Refresh:0.0001;url=login.php');
echo "<script> alert('非法访问,小心我告你')</script>";
exit();
}
include 'DBConn.php';
//根据所传参数判断是修改请求还是添加请求
if(isset($_POST["name"])&&isset($_POST["sex"])&&isset($_POST["country"])&&isset($_POST["hobby"])&&isset($_POST["pass"])){
$name=$_POST["name"];
$sex=$_POST["sex"];
$country=$_POST["country"];
$hobby=$_POST["hobby"];
$hobbies=implode(",",$hobby);
$pass=$_POST["pass"];
if($_POST["id"]!=null){//修改
$id=$_POST["id"];
$sqlupdate = "UPDATE student SET NAME='$name',sex='$sex',country='$country',hobby='$hobbies',password='$pass' WHERE id=$id";
if (mysqli_query($conn, $sqlupdate)) {
echo "<script>alert('修改成功')</script>";
header("Location: stuController.php"); //刷新当前页面
mysqli_close($conn);
} else {
echo "Error: " . $sqlupdate . "<br>" . mysqli_error($conn);
}
}else{//添加
$sql = "INSERT into student (name,sex,country,hobby,password)
VALUES ('$name','$sex','$country','$hobbies','$pass')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('新纪录插入成功')</script>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
//查询,返回全部结果
$sqlselect="select id,name,sex,country,hobby from student";
$result=mysqli_query($conn, $sqlselect);
if($result->num_rows>0){
echo "<h1>学生信息显示</h1>";
echo "<div><h3>当前登录用户:".$_SESSION['user']."      ";
echo "<a class='btn' href='login.php'>退出登录</a><h3></div>";
echo "<div><a href='student_add.php'>添加学生</a></div><br>";
echo "<table><tr><th>姓名</th><th>性别</th><th>国家</th><th>爱好</th><th>操作</th></tr>";
while($row=$result->fetch_assoc()){
echo '<tr><td>'.$row["name"].'</td>
<td>'.$row["sex"].'</td>
<td>'.$row["country"].'</td>
<td>'.$row["hobby"].'</td>
<td>
<a href="stuController.php?id='.$row["id"].'&func=delete">删除</a>'.' '.
'<a href="student_add.php?id='.$row["id"].'&func=update">修改</a></td></tr>';
}
echo "</table>";
}else{
echo "0个结果";
}
//删除业务,接受本页面传来的id参数,利用此参数删除对应记录
if(isset($_GET["id"])&&$_GET["func"]==delete){
$id=$_GET["id"];
$sqldelete='delete from student where id='.$id;
if (mysqli_query($conn, $sqldelete)) {
echo "<script>alert('删除成功')</script>";
mysqli_close($conn);
header("Location: stuController.php"); //刷新当前页面
} else {
echo "Error: " . $sqldelete . "<br>" . mysqli_error($conn);
}
}
mysqli_close($conn);
echo '
<style type="text/css">
body{text-align: center;}
table{
width:600px;height:370px;
border:1px solid black;/*设置边框粗细,实线,颜色*/
text-align:center;/*文本居中*/
background-color:#70DB93;
border-collapse: collapse;/*边框重叠,否则你会看到双实线*/
margin: auto;
}
th{
border:1px solid black;
color:black;
font-weight:bold;/*因为是标题栏,加粗显示*/
}
td{
border:1px solid black;
color:#8E2323;
}
a{
font-family: Arial;
margin: 3px;
}
a:LINK,a:VISITED {
color:#A62020;
padding:4px 10px 4px 10px;
background-color:#DDD;
text-decoration: none;
border-top: 1px solid #EEEEEE;
border-left: 1px solid #EEEEEE;
border-bottom: 1px solid #717171;
border-right: 1px solid #717171;
}
a:HOVER {
color: #821818;
padding: 5px 8px 3px 12px;
background-color: #CCC;
border-top: 1px solid #717171;
border-left: 1px solid #717171;
border-bottom: 1px solid #EEEEEE;
border-right: 1px solid #EEEEEE;
}
h1{
background-color:#678;
color:white;
text-align:center;
}
div{
text-align:center
}
.btn {
border: none;
color: red;
font-family:Arial;
padding: 10px 24px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 10px;
margin: 4px 2px;
cursor: pointer;
}
</style>';
?>
- 1
- 2
- 3
前往页