闲来无事,受老师所托写个php小案例方便讲课。单身狗的我就利用这一周六下午苦干了几个小时弄出来了,给大家分享一下呗。
技术总结:
1)没有mvc架构,没有用模板引擎,几乎thinkphp里的所有技术都没有用到,没办法呀,用了咋讲课?
2)session技术实现登录界面和防止越权访问界面
3)php连数据库mysql
项目结构:
效果显示:
1.登录界面:
2)登录成功:
3)添加页面:
4)修改页面:
5)还有一些其他的零碎的功能,可自己去玩玩哦!
项目源码:
sql文件:
/*
SQLyog 企业版 - MySQL GUI v8.14
MySQL - 5.5.5-10.3.7-MariaDB : Database - studb
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`studb` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `studb`;
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`sex` varchar(6) DEFAULT NULL,
`country` varchar(20) DEFAULT NULL,
`hobby` varchar(30) DEFAULT NULL,
`password` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=gbk;
/*Data for the table `student` */
insert into `student`(`id`,`name`,`sex`,`country`,`hobby`,`password`) values (1,'Mike','male','USA','basketball,football','146890'),(2,'Maria','female','USA','swim,football','146890'),(3,'liming','male','China','basketball','12'),(4,'Jane','female','Canada','swim,football','146890'),(6,'John','male','UK','swim,football','123456'),(7,'Json','male','UK','swim','146890'),(10,'kobe','male','USA','swim,basketball','146890'),(11,'admin','male','Local','swim,basketball,football','123456');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
stuController.php
session_start();
if(!isset($_SESSION['user'])){
header('Refresh:0.0001;url=login.php');
echo "";
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 "";
header("Location: stuController.php"); //刷新当前页面
mysqli_close($conn);
} else {
echo "Error: " . $sqlupdate . "
" . 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 "";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
}
}
//查询,返回全部结果
$sqlselect="select id,name,sex,country,hobby from student";
$result=mysqli_query($conn, $sqlselect);
if($result->num_rows>0){
echo "学生信息显示
";
echo "当前登录用户:".$_SESSION['user']."      ";
echo "退出登录
";
echo "
";
echo "姓名 性别 国家 爱好 操作 ";
while($row=$result->fetch_assoc()){
echo ''.$row["name"].'
'.$row["sex"].'
'.$row["country"].'
'.$row["hobby"].'
.$row["id"].'&func=delete">删除'.' '.
'.$row["id"].'&func=update">修改 ';
}
echo "
";
}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 "";
mysqli_close($conn);
header("Location: stuController.php"); //刷新当前页面
} else {
echo "Error: " . $sqldelete . "
" . mysqli_error($conn);
}
}
mysqli_close($conn);
echo '
';
?>
student_add.php
session_start();
if(!isset($_SESSION['user'])){
header('Refresh:0.0001;url=login.php');
echo "";
exit();
}
include 'DBConn.php'; ?>
<html>
<head>
<meta charset="utf-8">
<title>学生信息</title>
</head>
<body>
<h1>学生信息表单</h1>
<h3>当前登录用户: echo $_SESSION['user']?></h3>
<div>
<form method="post" action="stuController.php" onSubmit="return check();">
姓名:<input type="text" id="name" name="name"/><br/>
性别:
男<input type="radio" id="male" name="sex" value="male"/>
女<input type="radio" id="female" name="sex" value="female"/>
<br/>
国家:<input type="text" id="country" name="country"/><br/>
爱好:
游泳<input type="checkbox" id="h1" name="hobby[]" value="swim"/>
篮球<input type="checkbox" id="h2" name="hobby[]" value="basketball"/>
足球<input type="checkbox" id="h3" name="hobby[]" value="football"/><br/>
登录密码:<input type="password" id="pass" name="pass"/><br/>
确认密码:<input type="password" id="pass2" name="pass2"/><br/>
<input type="hidden" id="id" name="id" value=""/>
<br>
<input type="submit" value="提交" />
</form>
</div>
<script type="text/javascript">
function check(){
var pass=document.getElementById('pass').value;
var pass2=document.getElementById('pass2').value;
if(pass==pass2){
return true;
}else{
alert("两次密码不一致");
document.getElementById('pass').value="";
document.getElementById('pass2').value="";
return false;
}
}
</script>
if(isset($_GET["id"])&&$_GET["func"]=="update"){
$id=$_GET["id"];
$sqlSelectId="select * from student where id=".$id;
$result=mysqli_query($conn, $sqlSelectId);
$row=$result->fetch_assoc();
$name=$row["name"];
$sex=$row["sex"];
$country=$row["country"];
$hobby=$row["hobby"];
$hobbies=explode(",", $hobby);//将字符串按“,”,划分为数组
$pass=$row["password"];
echo "
";
if($sex=='male'){
echo "
";
}else{
echo "
";
}
for($i=0;$i<count($hobbies);$i++){
if($hobbies[$i]=='swim'){
echo "
";
}else if($hobbies[$i]=='basketball'){
echo "
";
}else if($hobbies[$i]=='football'){
echo "
";
}
}
}
?>
<style type="text/css">
h1{
background-color:#678;
color:white;
text-align:center;
}
body {
height: 100%;
width: 100%;
border: none;
overflow-x: hidden;
}
div{
width:100%;
text-align:center;
}
</style>
</body>
</html>
DBConn.php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "studb";
// 创建连接
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// 检测连接
if (!$conn) {
die("Connectionfailed: " . mysqli_connect_error());
}
?>
login.php
if(isset($_SESSION)){
session_start();
// 这种方法是将原来注册的某个变量销毁
unset($_SESSION['admin']);
// 这种方法是销毁整个 Session 文件
session_destroy();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
<link rel="stylesheet" href="SignUpStyle.css">
<link href="https://fonts.googleapis.com/css?familymPermanent+Marker" >
</head>
<body>
<div class="sign-div">
<form class="" action="check.php" method="post">
<h1>用户登录</h1>
<input class="sign-text" type="text" name="user" placeholder="用户名" >
<input class="sign-text" type="password" name="pass" placeholder="密码">
<h2>欢迎来到 学生信息管理系统</h2>
<input type="submit" value="登录"/>
</form>
</div>
<style>
body{
margin: 0;
padding: 0;
background: #487eb0;
}
.sign-div{
width: 300px;
padding: 20px;
text-align: center;
background: url(bg02.jpg);
position:absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
overflow: hidden;
}
.sign-div h1 ,h2{
margin-top: 100px;
color: #fff;
font-size: 40px;
}
.sign-div input{
display: block;
width: 100%;
padding: 0 16px;
height: 44px;
text-align: center;
box-sizing: border-box;
outline: none;
border: none;
font-family: "montserrat",sans-serif;
}
.sign-text{
margin:4px;
background: rgba(255,255,255,5);
border-radius: 6px;
}
.sign-btn:hover{
transform:scale(0.96);
}
.sign-div a{
text-decoration: none;
color: #fff;
font-family: "montserrat", sans-serif;
font-size: 14px;
padding: 10px;
transition: 0.8s;
display: block;
}
.sign-div a:hover{
background: rgba(0,0,0,.3);
}
</style>
</body>
</html>
check.php
include 'DBConn.php';
// 接收表单提交的用户名密码
$user = $_POST['user'];
$pass = $_POST['pass'];
//从数据库查询用户名和密码
$sqlsel="select name,password from student where name='$user' and password='$pass'";
$result=mysqli_query($conn, $sqlsel);
// 暂时给定合法用户为 张三 admin
if($result->num_rows==1){
session_start();
$_SESSION['user'] = $user;
header("Refresh:0.0001;url=stuController.php");
echo "";
exit();
}else{
header("Refresh:0.0001;url=login.php");
echo "";
exit();
}