js+php的简单学生管理系统,自己建个students的数据库
实现的效果如下图所示:
注册
登录
系统主页
新增页面
修改页面
注册页面--register.html
Documenthtml,body{
width: 100%;
height: 100%;
}
body{
background: url(./1.png) no-repeat 100% 100%;
}
body .container{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#register{
width: 400px;
}
.panel-footer{
display: flex;
/* 垂直方向 */
align-items: center;
/* 水平方向 */
justify-content: space-between;
}
.alert{
margin: 0px;
padding: 2px;
text-align: center;
display: none;
}
注册
用户名:
密码:
确认密码:
import {$post, antiShake} from "./ajax.js";
var oBtn = document.querySelector("#registerBtn");
var aInputs = document.querySelectorAll(".panel-body input");
var aAlert = document.querySelector(".alert");
//函数防抖
oBtn.onclick = antiShake(download, 1000);
function download(){
$post({
url: "register.php",
data: {
username: aInputs[0].value,
password: aInputs[1].value,
repassword: aInputs[2].value,
createTime: (new Date().getTime())
},
success: function(result){
// console.log(result);
var obj = JSON.parse(result);
aAlert.style.display = 'block';
if(obj.code){
//错误
aAlert.className = 'alert alert-danger';
aAlert.innerHTML = obj.msg;
}else{
aAlert.className = 'alert alert-success';
//成功
aAlert.innerHTML = obj.msg;
setTimeout(() => {
location.assign("login.html");
}, 500);
}
},
error: function(msg){
console.log(msg);
}
})
}
注册的php代码-register.php
header('content-type:text/html;charset="utf-8"');
$responseData = array("code" => 0, "msg" => "");
/*
在后台再进行一次数据校验
*/
$username = $_POST['username'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$createTime = $_POST['createTime'];
//1、判断用户名是否存在
if(!$username){
$responseData['code'] = 1;
$responseData['msg'] = "用户名不能为空";
echo json_encode($responseData);
exit;
}
if(!$password){
$responseData['code'] = 2;
$responseData['msg'] = "密码不能为空";
echo json_encode($responseData);
exit;
}
if($password != $repassword){
$responseData['code'] = 3;
$responseData['msg'] = "两次输入不一致";
echo json_encode($responseData);
exit;
}
$link = mysqli_connect("127.0.0.1", "root", "123456abc");
if(!$link){
$responseData['code'] = 4;
$responseData['msg'] = "服务器忙";
echo json_encode($responseData);
exit;
}
mysqli_set_charset($link, "utf8");
mysqli_select_db($link, "qd2002");
$sql = "SELECT * FROM users WHERE username='{$username}'";
//mysql result
$res = mysqli_query($link, $sql);
//取出一行
$row = mysqli_fetch_assoc($res);
//已经注册
if($row){
$responseData['code'] = 5;
$responseData['msg'] = "用户名已经存在";
echo json_encode($responseData);
exit;
}
//加密
$str = md5(md5(md5($password).'qianfeng').'xiaoming');
//准备sql,插入
$sql2 = "INSERT INTO users (username,password,createTime) VALUES('{$username}','{$str}',{$createTime})";
$res = mysqli_query($link, $sql2);
if($res){
$responseData['msg'] = "注册成功";
echo json_encode($responseData);
}else{
$responseData['code'] = 6;
$responseData['msg'] = "注册失败";
echo json_encode($responseData);
exit;
}
mysqli_close($link);
?>
登录页面-login.html
Documenthtml,body{
width: 100%;
height: 100%;
}
body{
background: url(./1.png) no-repeat 100% 100%;
}
body .container{
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#login{
width: 400px;
}
.panel-footer{
display: flex;
/* 垂直方向 */
align-items: center;
/* 水平方向 */
justify-content: space-between;
}
.alert{
margin: 0px;
padding: 2px;
text-align: center;
display: none;
}
登陆
用户名:
密码:
import {$post, antiShake} from "./ajax.js";
var oBtn = document.querySelector("#loginBtn");
var aInputs = document.querySelectorAll(".panel-body input");
var aAlert = document.querySelector(".alert");
//函数防抖
oBtn.onclick = antiShake(download, 1000);
function download(){
$post({
url: "login.php",
data: {
username: aInputs[0].value,
password: aInputs[1].value,
},
success: function(result){
// console.log(result);
var obj = JSON.parse(result);
aAlert.style.display = 'block';
if(obj.code){
//错误
aAlert.className = 'alert alert-danger';
aAlert.innerHTML = obj.msg;
}else{
aAlert.className = 'alert alert-success';
//成功
aAlert.innerHTML = obj.msg;
}
},
error: function(msg){
console.log(msg);
}
})
}
登录页面-login.php
header('content-type:text/html;charset="utf-8"');
$responseData = array("code" => 0, "msg" => "");
/*
在后台再进行一次数据校验
*/
$username = $_POST['username'];
$password = $_POST['password'];
//1、判断用户名是否存在
if(!$username){
$responseData['code'] = 1;
$responseData['msg'] = "用户名不能为空";
echo json_encode($responseData);
exit;
}
if(!$password){
$responseData['code'] = 2;
$responseData['msg'] = "密码不能为空";
echo json_encode($responseData);
exit;
}
//天龙八部
//1、链接数据库
$link = mysqli_connect("127.0.0.1", "root", "123456abc");
//2、判断数据库是否链接成功
if(!$link){
$responseData['code'] = 4;
$responseData['msg'] = "服务器忙";
echo json_encode($responseData);
exit;
}
//3、设置访问字符集
mysqli_set_charset($link, "utf8");
//4、选择我们要访问的数据库
mysqli_select_db($link, "qd2002");
//5、准备sql语句,进行登陆
//加密
$str = md5(md5(md5($password).'qianfeng').'xiaoming');
$sql = "SELECT * from users WHERE username='{$username}' AND password='{$str}'";
//6、发送sql语句
$res = mysqli_query($link, $sql);
//查询,取出其中的行
$row = mysqli_fetch_assoc($res);
// var_dump($row);
if($row){
$responseData['msg'] = "登陆成功";
echo json_encode($responseData);
}else{
$responseData['code'] = 5;
$responseData['msg'] = "用户名或密码错误";
echo json_encode($responseData);
exit;
}
mysqli_close($link);
?>
主页面 show.html
<
Document.box{
box-sizing: border-box;
width: 100%;
height: 100%;
padding: 100px;
position: absolute;
top: 0%;
left: 0%;
background-color: rgba(200, 200, 200, .7);
display: none;
}
.panel-footer{
display: flex;
justify-content: center;
align-items: center;
}
学生成绩管理系统
获取学生成绩
添加学生成绩
学生学号学生姓名语文成绩数学成绩英语成绩操作
学生姓名:
语文成绩:
数学成绩:
英语成绩:
取消
确定
import {post} from "./ajax.js";
var oBtain = document.querySelector(".select");
var oAdd = document.querySelector(".add")
var oTb = document.querySelector("#tb");
var oBox = document.querySelector(".box");
var oCancel =document.querySelector(".cancel");
var oSubmit = document.getElementById("submit");
//展示信息
function show(){
post({
url:"show.php",
success:function(result){
var arr = JSON.parse(result);
var str = '';
for(var i = 0;i
str +=`
${arr[i].id}${arr[i].username}${arr[i].chinese}${arr[i].math}${arr[i].english}删除修改`}
oTb.innerHTML=str;
},
error:function(error){
console.log(error)
}
})
}
oBtain.onclick = function(){
show()
}
var aIput = document.querySelectorAll("input");
//插入信息
oAdd.onclick = function(){
oBox.style.display="block";
oSubmit.onclick = function(){
post({
url:"insert.php",
data:{
username:aIput[0].value,
chinese:aIput[1].value,
math:aIput[2].value,
english:aIput[3].value,
},
success:function(result){
if(result=="success"){
alert("添加成功")
}else{
alert("添加失败,请重试")
}
},
error:function(error){
console.log(error)
}
})
}
}
oCancel.onclick = function(){
oBox.style.display="none";
}
//删除和修改学生信息
oTb.onclick = function(eve){
var e = eve||window.event;
var target = e.target ||e.srcElement;
if(target.nodeName.toLocaleLowerCase() == 'button'){
switch(target.className){
//删除
case 'btn btn-danger':
post({
url:"delete.php",
data:{
id:target.id
},
success:function(result){
if(result=="success"){
oTb.removeChild(target.parentNode.parentNode);
alert("删除成功")
}else{
alert("删除失败")
}
}
});
break;
//修改
case 'btn btn-warning':
post({
url:"update.php",
data:{
id:target.id
},
success:function(result){
var arr = JSON.parse(result);
oBox.style.display="block";
aIput[0].value=arr[0].username,
aIput[1].value=arr[0].chinese,
aIput[2].value=arr[0].math,
aIput[3].value=arr[0].english;
var id = arr[0].id;
oSubmit.οnclick=function(){
post({
url:"update1.php",
data:{
id:id,
username:aIput[0].value,
chinese:aIput[1].value,
math:aIput[2].value,
english:aIput[3].value,
},
success:function(result){
if(result=="success"){
alert("修改成功")
}else{
alert("请重新修改")
}
}
})
}
}
})
}
}
}
通过ajax传数据-ajax.js
export{
post,
get,
ajax
}
function post(){
var argus =Object.assign({"method" :"post"},...arguments)
var g = new Ajax(argus);
g.init();
g.type()
}
function get(){
var argus =Object.assign({"method" :"get"},...arguments)
var g = new Ajax(argus);
g.init();
g.type()
}
// function ajax(){
// var a =new Ajax(...arguments);
// a.init();
// a.type();
// }
function ajax(){
var a = new Ajax(...arguments);
a.init();
a.type()
}
class Ajax{
constructor({method,url,data,success,error}){
this.method = method;
this.url = url;
this.data = data;
this.success = success;
this.error = error;
}
init(){
var xhr = null;
try{
xhr = new XMLHttpRequest;
}catch{
xhr = new ActiveXObject("XMLHTTP");
}
return xhr;
}
type(){
var xhr= this.init()
var querystring=""
if(this.data){
querystring = this.queryString(this.data);
}
if(this.method=="get"){
xhr.open(this.method,this.url+"?"+querystring,true);
xhr.send();
}else{
xhr.open(this.method,this.url,true);
xhr.setRequestHeader('content-type', "application/x-www-form-urlencoded");
xhr.send(querystring);
}
xhr.onreadystatechange = ()=>{
if(xhr.readyState ==4){
if(xhr.status==200){
if(this.success){
this.success(xhr.responseText)
}
}else{
if(this.error){
this.error("error"+xhr.status)
}
}
}
}
}
queryString(dataObj){
var str='';
for(var attr in dataObj){
str+=`${attr}=${dataObj[attr]}&`
}
return str.substring(0,str.length-1)
}
}
分页-paging.php
header("content-type:text/html,charset='utf8'");
$page = $_POST['page'] ? $_POST['page'] :1;
$limit = $_POST['limit'] ? $_POST['limit']:10;
$count = 0; //总条数
$pages = 0; //总页数
$skip = 0; //跳过的条数
$link=mysqli_connect("localhost","root","root");
if(!$link){
echo "服务器忙,请重试";
exit;
}
mysqli_set_charset($link,"utf8");
mysqli_select_db($link,"nz");
$sql = "SELECT COUNT(*) FROM students";
$res=mysqli_query($link,$sql);
$row = mysqli_fetch_assoc($res);
$count = $row['COUNT(*)'];
// echo $count;
$pages = ceil($count / $limit);
$page = max(1, $page);
$page = min($page, $pages);
$skip = ($page -1) * $limit;
$msg =array(
"page" => $page,
"count" => $count,
"pages" => $pages,
"limit" => $limit,
"skip" => $skip,
"userlist" => array()
);
$sql2 = "SELECT * FROM students LIMIT {$skip}, {$limit}";
$res2 = mysqli_query($link, $sql2);
while($row2 = mysqli_fetch_assoc($res2)){
array_push($msg['userlist'], $row2);
}
echo json_encode($msg);
mysqli_close($link);
?>
数据添加 insert.php
header("content-type:text/html,charset='utf8'");
$username = $_POST['username'];
$chinese = $_POST['chinese'];
$math = $_POST['math'];
$english = $_POST['english'];
$link = mysqli_connect("localhost","root","root");
if(!$link){
echo "服务器忙,请稍后重试";
exit;
};
mysqli_set_charset($link,"utf8");
mysqli_select_db($link,"nz");
$sql ="insert into students (username,chinese,math,english) values('{$username}','{$chinese}','{$math}','{$english}')";
$res = mysqli_query($link,$sql);
if($res){
echo "success";
}else{
echo "error";
};
mysqli_close($link);
?>
数据删除 delete.php
header('content-type:text/html,charset="utf8"');
$id=$_POST['id'];
$link=mysqli_connect("localhost","root","root");
if(!$link){
echo "服务器忙,请重试";
exit;
};
mysqli_set_charset($link,"utf8");
mysqli_select_db($link,"nz");
$sql="delete from students where id={$id}";
$res = mysqli_query($link,$sql);
if($res){
echo "success";
}else{
echo "error";
};
mysqli_close($link);
?>
数据修改-查找update.php
header("content-type:text/html,charset='utf8'");
$id=$_POST['id'];
$link=mysqli_connect("localhost","root","root");
if(!$link){
echo "服务器忙,请重试";
exit;
}
mysqli_set_charset($link,"utf8");
mysqli_select_db($link,"nz");
$sql="select * from students where id ='{$id}'";
$res=mysqli_query($link,$sql);
$arr = array();
while($row=mysqli_fetch_assoc($res)){
array_push($arr,$row);
};
echo json_encode($arr);
mysqli_close($link);
?>
数据修改-更新 update1.php
header("content-type:text/html,charset='utf8'");
$id = $_POST['id'];
$username = $_POST['username'];
$chinese = $_POST['chinese'];
$math = $_POST['math'];
$english = $_POST['english'];
$link=mysqli_connect("localhost","root","root");
if(!$link){
echo "服务器忙,请重试";
exit;
}
mysqli_set_charset($link,"utf8");
mysqli_select_db($link,"nz");
// $sql="delete from students where id={$id}";
$sql = "UPDATE students SET username ={$username} ,chinese ={$chinese},math={$math},english={$english} where id={$id}";
$res=mysqli_query($link,$sql);
if($res){
echo "success";
}else{
echo "error";
};
mysqli_close($link);
?>