实现注册,登录,修改信息,删除,添加信息,使用mysql数据库,记住会话(session)等功能
注册页面
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="register.php" method="POST" >
<table border="1">
<tr>
<td>学号</td>
<td><input type="text" name="id" ></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td>性别</td>
<td>男<input type="radio" name="gender" value="男">
女<input type="radio" name="gender" value="女"></td>
</tr>
<tr>
<td><input type="submit" name ='submit' value="注册" ></td>
<td><input type="reset" value="重置"> </td>
</tr>
</table>
</form>
</body>
</html>
register.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 8:44
*/
header('content-type:text/html;charset=utf-8');
include 'insert.php';
if(isset($_POST['submit'])) {
$user = $_POST['username'];
$gender = $_POST['gender'];
$password=$_POST['password'];
$tel = $_POST['tel'];
$id = $_POST['id'];
if($user!='admin'){
$s=new insert();
$s->reg_add($id,$user,$password,$gender,$tel);
}else echo '此用户不能被注册';}else{
header("Location:register.html");
}
?>
登录页面
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script language="JavaScript">
function reg() {
location='register.html'
}
</script>
<body>
<div style="position: absolute;left: 50%;top:50%;width: 500px;margin-left: -250px;margin-top: -200px">
<div style="background: #eFeFeF;padding:20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444">
<h2>学生信息管理系统</h2>
<form action="login.php" method="post">
<div><label>用户名</label>
<div><input type="text" name="username">
</div>
</div>
<div>
<label>id</label>
<div><input type="text" name="id"></div>
</div>
<div><label>密码</label>
<div><input type="password" name="password">
</div>
</div>
<div><div><input type="submit" value="登录" name="denglu">
<input type="button" value="注册" onclick="reg()"></div>
<div></div></div>
</form>
</body>
</html>
login.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 11:15
*/
include 'select.php';
header('content-type:text/html;charset=utf-8');
if(isset($_POST['denglu'])) {
session_start();
$user = $_POST['username'];
$password=$_POST['password'];
$id=$_POST['id'];
$_SESSION['username'] = $user;
$_SESSION['id']=$id;
$a=new select();
$a->deng_select($user,$password,$id);
}else{
header('location;logon.php');
}
?>
登录进入的首页
index.php
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<table border="1" width="960">
<caption><h2>学生信息管理系统</h2></caption>
<th colspan="2"><a href="add.php">添加</a> </th>
<th colspan="1"><a href="quit.php">退出登录</a></th>
<th colspan="2"><a href="search.php">查找</a></th>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
<th>操作</th>
</tr>
<?php
session_start();
include "select.php";
if(isset($_SESSION['username'])&&isset($_SESSION['id'])) {
$user = $_SESSION['username'];
$id = $_SESSION['id'];
$_SESSION['username'] = $user;
echo "<h1>欢迎登录,$user</h1>";
if ($user == 'admin') {
$b = new select();
$b->all_select();
} else {
$a = new select();
$a->one_select($id);
}
}else{
echo '<script>alert(`未登录`);location="login.html"</script>';
}?>
</body>
</html>
添加功能的实现
add.php
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="POST" >
<table border="1">
<tr>
<td>学号</td>
<td><input type="text" name="id" ></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td>性别</td>
<td>男<input type="radio" name="gender" value="男">
女<input type="radio" name="gender" value="女"></td>
</tr>
<tr>
<td><input type="submit" name ='submit' value="添加" ></td>
<td><input type="reset" value="重置"> </td>
</tr>
</table>
</form>
</body>
</html>
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 16:02
*/
session_start();
$user=$_SESSION['username'];
include 'insert.php';
if($user==='admin'){
$user = $_POST['username'];
$gender = $_POST['gender'];
$password=$_POST['password'];
$tel = $_POST['tel'];
$id = $_POST['id'];
$a=new insert();
$a->admin_add($id,$user,$password,$gender,$tel);
}else{
echo '不是admin用户,没有权限操作';
}
?>
删除功能
delete.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 15:06
*/
header('content-type:text/html;charset=utf-8');
class delete{
public function user_delete($id){
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$sql="delete from users where id=$id";
if(mysqli_query($conn,$sql)){
echo "<script>alert(`删除成功`);location='index.php'</script>";
}
}
}
session_start();
$user=$_SESSION['username'];
if($user=='admin') {
$id = $_GET['id'];
$a = new delete();
$a->user_delete($id);
}
?>
插入功能实现
insert.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 8:52
*/
class insert{
public function reg_add($id,$user,$password,$gender,$tel)
{
mysqli_set_charset(mysqli_connect('localhost', 'root', 'root', 'student'), 'utf-8');
$sql = "insert into users values($id,'$user','$password','$gender',$tel)";
if (mysqli_query(mysqli_connect('localhost', 'root', 'root', 'student'), $sql)) {
echo '<script>alert("注册成功");location="login.html"</script>';
} else {
echo '<script>alert("注册失败");location="registr.html"</script>';
}}
public
function admin_add($id, $user, $password, $gender, $tel)
{
mysqli_set_charset(mysqli_connect('localhost', 'root', 'root', 'student'), 'utf-8');
$sql = "insert into users values($id,'$user','$password','$gender',$tel)";
if (mysqli_query(mysqli_connect('localhost', 'root', 'root', 'student'), $sql)) {
echo '<script>alert("添加成功");location="index.php"</script>';
}
}
}
?>
退出功能
quit.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/28
* Time: 10:25
**/
session_start();
session_destroy();
session_unset();
header('location:login.html');
?>
查找功能
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="#" method="GET" >
id<input type="text" name="id">
<input type="submit" name ='submit' value="查找" >
<input type="reset" value="重置">
</form>
</body>
</html>
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 16:08
*/
header('content-type:text/html;charset=utf-8');
session_start();
include 'select.php';
$user=$_SESSION['username'];
if($user=='admin'){
if(isset($_GET['id'])){
$id=$_GET['id'];
echo '<table border="1" width="960">
<caption><h2>学生信息管理系统</h2></caption>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
</tr>';
$a=new select();
$a->one_select($id);
}}else{
echo '不是admin用户,没有权限操作';
}
?>
查询功能
select.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 10:12
*/
class select{
public function deng_select($username,$password,$id){
$sql="select * from users where password='$password' and username='$username' and id='$id'";
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)){
echo ' <script>alert("登录成功");location="index.php"</script>';
}else {
echo ' <script>alert("用户名或学号或密码错误,请重新登录");location="login.html"</script>';
}
}
public function one_select ($id){
$sq="select * from users where id='$id'";
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$rel=mysqli_query($conn,$sq);
$resul=mysqli_fetch_array($rel);
echo '<tr>';
echo "<td>{$resul['id']}</td>";
echo "<td>{$resul['username']}</td>";
echo "<td>{$resul['gender']}</td>";
echo "<td>{$resul['tel']}</td>";
echo '</tr>';
}
public function all_select(){
$sq="select * from users";
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$re=mysqli_query($conn,$sq);
$num=mysqli_query($conn,'select count(*) from users');
$num=mysqli_fetch_assoc($num);
$num=$num['count(*)'];
$i=1;
while($i<=$num){
$arr=mysqli_fetch_assoc($re);
echo '<tr>';
echo "<td>{$arr['id']}</td>";
echo "<td>{$arr['username']}</td>";
echo "<td>{$arr['gender']}</td>";
echo "<td>{$arr['tel']}</td>";
echo "<td>
<a href='delete.php?id={$arr['id']}'>删除</a>
<a href='xiu.php?id={$arr['id']}'>修改</a>
</td>";
echo '</tr>';
$i++;
}
}
public function update_select ($id){
$s="select * from users where id='$id'";
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$rel=mysqli_query($conn,$s);
$resul=mysqli_fetch_array($rel);
return $resul;
}
}
?>
修改功能
xiu.php
<?php
/**
* Created by PhpStorm.
* User: mushangqiujin
* Date: 2022/12/27
* Time: 15:06
*/
header('content-type:text/html;charset=utf-8');
$id_f=$_GET['id'];
class upda{
public function update($id_f,$id,$user,$password,$gender,$tel){
$conn=mysqli_connect('localhost','root','root','student');
mysqli_set_charset($conn,'utf-8');
$sql="update users set id=$id ,username='$user',password='$password',gender='$gender',tel='$tel' where id=$id_f ";
if(mysqli_query($conn,$sql)){
echo '<script>alert("修改成功");location="index.php"</script>';
}
}
}
include 'select.php';
$a=new select();
$resul=($a->update_select($id_f));
//$sq="select * from users where id='$id_f'";
//$conn=mysqli_connect('localhost','root','root','student');
//mysqli_set_charset($conn,'utf-8');
//$rel=mysqli_query($conn,$sq);
//$resul=mysqli_fetch_array($rel);
//echo $sq;
if(isset($_POST['submit']))
{
$user = $_POST['username'];
$gender = $_POST['gender'];
$password=$_POST['password'];
$tel = $_POST['tel'];
$id = $_POST['id1'];
$c=new upda();
$c->update($id_f,$id,$user,$password,$gender,$tel);
}
?>
<form action="#" method="POST" >
<table border="1">
<tr>
<td>学号</td>
<td><input type="text" name="id1" value="<?php echo $resul['id'] ?>"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="username" value="<?php echo $resul['username'] ?>"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" value="<?php echo $resul['password'] ?>"></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="tel" value="<?php echo $resul['tel'] ?>"></td>
</tr>
<tr>
<td>性别</td>
<td>男<input type="radio"<?php if($resul['gender']=='男')echo 'checked'?> name="gender" value="男">
女<input type="radio" <?php if($resul['gender']=='女')echo 'checked'?>name="gender" value="女"></td>
</tr>
<tr>
<td><input type="submit" name ='submit' value="修改" ></td>
<td><input type="reset" value="重置"> </td>
</tr>
</table>
</form>
</body>
</html>
实现效果
参考文章