PHP实战小案例--《信息管理系统》(附源码)
闲来无事,受老师所托写个php小案例方便讲课。单身狗的我就利用这一周六下午苦干了几个小时弄出来了,给大家分享一下呗。技术总结:1)没有mvc架构,没有用模板引擎,几乎thinkphp里的所有技术都没有用到,没办法呀,用了咋讲课?2)session技术实现登录界面和防止越权访问界面3)php连数据库mysql项目结构:效果显示:1.登录界面:2)登录成功:3)添加页面:4)修改页面:5)还有一些其他
·
闲来无事,受老师所托写个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
<?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:300px;
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>';
?>
student_add.php
<?php
session_start();
if(!isset($_SESSION['user'])){
header('Refresh:0.0001;url=login.php');
echo "<script> alert('非法访问,小心我告你')</script>";
exit();
}
include 'DBConn.php'; ?>
<html>
<head>
<meta charset="utf-8">
<title>学生信息</title>
</head>
<body>
<h1>学生信息表单</h1>
<h3>当前登录用户:<?php 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>
<?php
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 "
<script>
document.getElementById('name').value='$name';
document.getElementById('country').value='$country';
document.getElementById('id').value=$id;
document.getElementById('pass').value=$pass;
document.getElementById('pass2').value=$pass;
</script>";
if($sex=='male'){
echo "
<script>
document.getElementById('male').checked=true;
</script>";
}else{
echo "
<script>
document.getElementById('female').checked=true;
</script>";
}
for($i=0;$i<count($hobbies);$i++){
if($hobbies[$i]=='swim'){
echo "
<script>
document.getElementById('h1').checked=true;
</script>";
}else if($hobbies[$i]=='basketball'){
echo "
<script>
document.getElementById('h2').checked=true;
</script>";
}else if($hobbies[$i]=='football'){
echo "
<script>
document.getElementById('h3').checked=true;
</script>";
}
}
}
?>
<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
<?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
<?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
<?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 "<script> alert('登录成功')</script>";
exit();
}else{
header("Refresh:0.0001;url=login.php");
echo "<script> alert('登录失败')</script>";
exit();
}
更多推荐
已为社区贡献1条内容
所有评论(0)