这是一个对于php初学者来说算是比较友好的小项目,借鉴了一些github上的一些代码,主要功能是实现一个登录查看数据库,对数据库进行增删改查的功能.,文件目录如下(注册暂时没写) 
效果展示:  查所有数据:  删除键:  更改:  增加: 
1.登录界面
<?php
session_start();
?>
<!DOCTYPE>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录界面</title>
</head>
<style>
#style1{
width:610px ;
height:500px ;
border:1px dashed #000000;
margin:50px auto;
padding: 5px;
background-color:;
}
#style1 h2{
text-align: center;
left: auto;
top: auto;
}
#style1 input{
width:200px;
height:30px;
border: 1px solid plum;
border-radius: 7px 7px;
outline: none;
float: right;
}
#style1 td{
width: 200px;
height: 60px;
}
#td1{
text-align: right;
}
#td2{
text-align: center;
}
</style>
<body>
<form id="style1" action="login_check.php">
<table>
<h2> 登录界面 </h2>
<tr>
<td id="td1">
username:
</td>
<td>
<input type="text" name="username" id="username">
</td>
</tr>
<tr>
<td id="td1">
password:
</td>
<td>
<input type="password" name="password" id="password">
</td>
<td>
<span style="display: block font-size:15px" id="normal" >6-16位大小写字母数字组合</span>
<span style="display: none" id="low">密码强度弱</span>
<span style="display: none" id="mid">密码强度中</span>
<span style="display: none" id="high">密码强度强</span>
<span style="display: none" id="illegal">含非法字符</span>
</td>
</tr>
<tr>
<td id="td1">
verification code:
</td>
<td>
<input type="text" name="code">
</td>
<td>
<img id="verification_img" border="1" src="code.php" alt="" width="100" height="30" onclick="change_image();" title="点击更换">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="登录" style="background-color: pink" >
</td>
</tr>
<tr>
<td></td>
<td style="padding-bottom: 40px">
<input style="background-color:skyblue" type="button" name="register" value="注册" onclick='location.href=("register.php")' >
</td>
</tr>
<tr>
<td></td>
<td style="color: red" id="td2">
<?php
echo $_SESSION['error'];
$_SESSION['error'] = "";
?>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
let img = document.getElementById("verification_img");
let username = document.getElementById('username');
let password = document.getElementById('password');
let illegal = document.getElementById('illegal');
let nomal = document.getElementById('normal')
let low = document.getElementById('low');
let mid = document.getElementById('mid');
let high = document.getElementById('high');
function addkeyevent(){
let low1 = /(^\d{6,16}$)|(^[a-z]{6,16}$)|(^[A-Z]{6,16}$)/g;
let mid1 = /(^[A-Za-z]{6,16}$)|(^[a-z\d]{6,16}$)|(^[A-Z\d]{6,16}$)/g;
let high1 = /(^[a-zA-Z\d]{6,16})/g;
if (password.value.length > 16 || password.value.length < 6){
nomal.style.display="block";
high.style.display="none";
mid.style.display="none";
low.style.display="none";
illegal.style.display="none";
}else{
if(low1.test(password.value)){
nomal.style.display="none";
high.style.display="none";
mid.style.display="none";
low.style.display="block";
illegal.style.display="none";
}else if(mid1.test(password.value)){
nomal.style.display="none";
high.style.display="none";
mid.style.display="block";
low.style.display="none";
illegal.style.display="none";
}else if(high1.test(password.value)) {
nomal.style.display="none";
high.style.display = "block";
mid.style.display = "none";
low.style.display = "none";
illegal.style.display="none";
}else{
nomal.style.display="none";
high.style.display = "none";
mid.style.display = "none";
low.style.display = "none";
illegal.style.display="block";
}
}
}
password.addEventListener('keyup',addkeyevent,false);
function change_image(){
img.setAttribute('src','code.php');
}
</script >
</body>
</html>
主要是一些js和css的操作,学习的时候因为对前端的知识也不算特别熟悉,所以就想练习一下这方面的知识,主要是实现了一些常见的验证啊之类的功能.
2.登录验证部分
<?php
session_start();
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$username = $_GET['username'];
$password = $_GET['password'];
$code = $_GET['code'];
$sql = "SELECT password FROM `users`
where username = '{$username}'";
$result = mysqli_query($con,$sql) or die("sql语句执行失败!");
$new = mysqli_fetch_assoc($result);
if($username == '' or $password == '' or $code ==''){
$_SESSION['error'] = "请填写完整账号,密码,验证码";
header("location:login.php");
}else{
if ($_SESSION['authcode'] == $code){
if ($password != $new['password']){
$_SESSION['error'] = "请输入正确的账号密码";
header("location:login.php");
}else{
$_SESSION['error'] = '登陆成功';
$_SESSION['check'] = '1';
header("location:index.php");
}
}else{
$_SESSION['error'] = "请输入正确的验证码";
header("location:login.php");
}
}
这部分先连接数据库将账号密码和数据库中的比对,正确则跳到index也就是能显示全部数据的界面,用到了session,可以先去了解一下session再来学习这部分,还有php的数据库连接知识.(需要先在数据库有users表里面有一些用户的账号密码数据)
3.验证码
<?php
//必须至于顶部,多服务器端记录验证码信息,便于用户输入后做校验
session_start();
//默认返回的是黑色的照片
$image = imagecreatetruecolor(100, 30);
//将背景设置为白色的
$bgcolor = imagecolorallocate($image, 255, 255, 255);
//将白色铺满地图
imagefill($image, 0, 0, $bgcolor);
//空字符串,每循环一次,追加到字符串后面
$captch_code='';
//验证码为随机四个数字
for ($i=0; $i < 4; $i++) {
$fontsize=6;
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
//产生随机数字0-9
$fontcontent = rand(0,9);
$captch_code.= $fontcontent;
//数字的位置,0,0是左上角。不能重合显示不完全
$x=($i*100/4)+rand(5,10);
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captch_code;
//为验证码增加干扰元素,控制好颜色,
//点
for ($i=0; $i < 200; $i++) {
$pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//为验证码增加干扰元素
//线
for ($i=0; $i < 3; $i++) {
$linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220));
imageline($image, rand(1,99), rand(1,29),rand(1,99), rand(1,29) ,$linecolor);
}
header('content-type:image/png');
imagepng($image);
//销毁
imagedestroy($image);
这部分主要是对GD库的运用,看一遍GD库的函数其实也差不多能理解它的使用方法,建议看一遍.或者也可以去网上看看其他人的代码怎么写,有一个地方就是要注意就是每次登录都需要把原来的验证码删除,因为如果不删除验证码跳转界面后按后退键验证码是不会变的,存在安全问题,这个处理没有在这里写,在其他部分写了
4.主界面
<?php
session_start();
if ($_SESSION['check'] != '1')
header("location:login.php");
$_SESSION['check'] = "";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生成绩管理系统</title>
</head>
<style type="text/css">
.style1{
width: 1200px;
margin: 10px auto;
}
h1{
text-align: center;
}
.style2{
margin-bottom: 10px;
}
.style2 a{
text-decoration:none;//加了这个去掉下划线,但是我设两个颜色好像都能去掉下划线
color: darkorange;
text-align: center;
padding: 10px;
background-color: aquamarine;
border-radius: 10px;//去掉四周尖角
}
td{
text-align: center;
}
td a{
text-decoration:none;
color: blue;
}
</style>
<body>
<div class="style1">
<h1>学生成绩管理系统</h1>
<div class="style2">
<a href="addscore.html">添加学生成绩记录</a>
</div>
<table width="1200px", border="1">
<tr>
<th>学号</th>
<th>课程号</th>
<th>成绩</th>
<th>学分</th>
<th>操作</th>
</tr>
<?php
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$sql = 'select * from xscj';
$result = mysqli_query($con,$sql);
//$rows是结果的行数,
$rows = mysqli_num_rows($result);
for($i = 0;$i < $rows;$i++){
$new = mysqli_fetch_assoc($result);//拿出一行数据
echo "<tr>";
echo "<td>{$new['ssid']}</td>";
echo "<td>{$new['cid']}</td>";
echo "<td>{$new['score']}</td>";
echo "<td>{$new['sscore']}</td>";
echo "<td>
<a href='javascript:del({$new['ssid']},{$new['cid']})'>删除</a>
<a href='update.php?xuehao={$new['ssid']}&kechenghao={$new['cid']}'>修改</a>
</td>";
echo "<tr>";
}
//释放结果集
mysqli_free_result($result);
mysqli_close($con);
?>
</table>
</div>
<script type="application/javascript">
function del (xuehao,kechenghao){ //delete会错 估计是有内置函数
if(confirm("确定要删除该学生的成绩记录?")){
window.location = "delete.php?xuehao="+xuehao+"&kechenghao="+kechenghao;
}
}
//这里有个小问题,一开始我的数据库里学号是以0开头的,把我的数据都解析成了8进制导致删除函数执行无法找到正确的学号,这里注意进制问题
</script>
</body>
</html>
前5行代码是后面自己优化的时候搞的验证,相当于是没有登录的话你的check为0,不能跳转到这个界面并且回到登录界面,算是一个小小的防范.其他基本就是一个数据库查询操作和一些js操作显示基本信息
5.增加数据
addscore.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加学生成绩界面</title>
</head>
<body>
<form action="insert.php" method="post">
学号:<input type="text" name="xuehao">
课程号:<input type="text" name="kechenghao">
成绩:<input type="text" name="chengji">
学分:<input type="text" name="xuefen">
<input type="submit" >
</form>
</body>
</html>
insert.php
<?php
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$xuehao = $_POST['xuehao'];
$kechenghao = $_POST['kechenghao'];
$chengji = $_POST['chengji'];
$xuefen = $_POST['xuefen'];
$sql = "INSERT INTO xscj (ssid, cid, score, sscore)
VALUES ($xuehao, $kechenghao, $chengji, $xuefen)";
mysqli_query($con,$sql) or die("无法做此修改!");
mysqli_close($con);
header("location:index.php");
这部分做的比较简单,就是把输进来的数据进行一个数据库的插入操作,熟悉了它的数据流向和数据库操作这部分不会太难
6.更改数据
update.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改学生成绩界面</title>
</head>
<body>
<?php
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$xuehao = $_GET['xuehao'];
$kechenghao = $_GET['kechenghao'];
$sql = "select * from xscj where ssid = {$xuehao} and cid = {$kechenghao}";
$result = mysqli_query($con,$sql);
$new = mysqli_fetch_assoc($result);
?>
<form action="update-action.php" method="post">
学号:<input type="text" name="xuehao" value="<?=$new['ssid'] ?>" readonly="readonly">
课程号:<input type="text" name="kechenghao" value="<?=$new['cid'] ?>" readonly="readonly">
成绩:<input type="text" name="chengji" value="<?=$new['score'] ?>">
学分:<input type="text" name="xuefen" value="<?=$new['sscore'] ?>">
<input type="submit" >
<br>注:只可以修改成绩与学分
</form>
</body>
</html>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
update-action.php
<?php
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$xuehao = $_POST['xuehao'];
$kechenghao = $_POST['kechenghao'];
$chengji = $_POST['chengji'];
$xuefen = $_POST['xuefen'];
$sql = "update xscj set score = {$chengji},sscore = {$xuefen} where ssid = {$xuehao} and cid = {$kechenghao}";
mysqli_query($con,$sql) or die("无法做此修改!");
mysqli_close($con);
header("location:index.php");
第一个是输入要更改的信息,输入的数据传到下面的文件进行数据库操作处理
7.删除数据
delete.php
<?php
require "define.php";
$con = mysqli_connect(host,user,password,dbname) or die("数据库连接失败!");
mysqli_set_charset($con,'utf8');
$xuehao = $_GET['xuehao'];
$kechenghao = $_GET['kechenghao'];
$sql = "delete from xscj where ssid = {$xuehao} and cid = {$kechenghao}";
mysqli_query($con,$sql) or die("删除出错!");
mysqli_close($con);
header("location:index.php");
删除数据库对应的数据
8.说点其他
一开始对新手来说学习一门新语言可能就是学学循环啥的那些,其实根本就学不到啥东西,这个demo给我自己的收获还是挺大的,一方面是了解了数据的交互方式,以前没学过的js,数据库部分相当于是复习学到的sql语句,还涉及到一些安全性能的小问题,总体来说这个demo我认为对一个刚刚入门的小白来说更快掌握php.
|