zl程序教程

您现在的位置是:首页 >  后端

当前栏目

php实现带权限的增删改查功能

PHP权限 实现 功能 增删 改查
2023-06-13 09:13:56 时间

作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人) 阿里云专家博主

文章目录


前言

原生php实现简易的招聘网站:逻辑流程如下: 第一:boss能增删改查(招聘内容)。求职者只能看招聘内容.(权限的不同) 第二:不登录不能查看。必须登录某个账号才能查看. 第三:防止重复提交 防止输出空数据 第四:多用户的哦

代码如下。

cyg.php(注册页面)

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>带权限的curd(招聘网站)</title>
</head>
<body>
	<form action="cyg.php" method="POST">
		名字:<input type="text" name="name" />
		管理员还是员工:
		<select name="type">
		      <option value="1" selected>员工</option>
		      <option value="2">管理员</option> 
		</select>
		<input type="submit" value="提交">
	</form>
</body>
</html>
<?php
$link=mysqli_connect('localhost','root','root','a');//链接数据库(数据库软件账号密码都是root.数据库是a)
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');//字符串utf8
$type=$_POST['type'];
$name=$_POST['name'];
$date=date('Y-m-d H:i:s');
$sql = "INSERT INTO qx(name,type,time)
VALUES ('{$name}','{$type}','{$date}')";
 
$result=mysqli_query($link,$sql);
if($result)
{
	$_SESSION['login']=1;
}
else
{
	$_SESSION['login']=0;
}
//上面这些都是注册boss或者求职者成功啦

cyg1.php(登录页面)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="cyg1.php" method="POST">
		登录用户名<input type="text" name="username">
		登录密码<input type="password" name="password">
		
		<input type="submit"  name="submit" >
	</form>
</body>
</html>
<?php
SESSION_START(); //防止表单重复提交
if (isset($_POST['submit'])) { 
    if ($_SESSION['is_submit'] == '0') { 
        $_SESSION['is_submit'] = '1'; 
        echo "代码块,要做的事,代码...<a οnclick='history.go(-1);' href='javascript:void(0)'>返回</a>"; 
    } else { 
        echo "请不用重复提交<a href='index.php'>PHP+SESSION防止表单重复提交</a>"; 
    } 
}
//用户输入的(表单传过来的)
$username=$_POST['username'];
$password=$_POST['password'];
if(!$username||!$password)
{
	return ;
}
//=========================
$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
//查询然后判断数据库中的用户名username+密码password是不是与表单传过来的$username+$password相匹配.
$sql="select type from qx where name='{$username}' and password='{$password}'";
$query=mysqli_query($link,$sql);//运行sql
$result=mysqli_fetch_array($query);//把对象变成数组,不然直接输出会报错
if($result)
{
	$_SESSION['login']=1;
}
else
{
	$_SESSION['login']=0;
}
if($result['type']==1)
{
	echo "<script>alert('求职者登录成功');location.href='cyg2.php';</script>";
}
else
{
	echo "<script>alert('BOSS登录成功');location.href='index.php';</script>";
	
}

BOSS登陆后的权限

查看权限

<?php
session_start();
if($_SESSION['login']!=1)
{
	echo "<script>alert('抱歉,用户还没登录');window.location.href='cyg1.php';</script>";
}
?>
<?php
$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql="select id,name from content";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql

?>
<!--显示的效果-->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<table border="1" cellpadding="5">
		<tr>
			<td>id</td>
			<td>内容</td>
		
		<?php 
			while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
				# code...
		
		?>
		<tr>
			<td><?=$row['id'];?></td>
			<td><?=$row['name'];?></td>
			<td><a href="update.php?id=<?php echo $row['id']; ?>">更新</a></td>
			<td><a href="delete.php?id=<?php echo $row['id']; ?>">删除</a></td>
			
		</tr>
		
		<?php 
		}
		?>
		<td><a href="create.php">创建</a></td>
		</tr>
		
	</table>
</body>
</html>

删除权限

<?php
session_start();
if($_SESSION['login']!=1)
{
	echo "<script>alert('抱歉,用户还没登录');window.location.href='cyg1.php';</script>";
}
?>
<?php
$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";

//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,"DELETE FROM content WHERE id ='$_GET[id]'");//运行sql
$sql="select * from content";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql

?>
<!--显示的效果-->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<table border="1" cellpadding="5">
		<tr>
			<td>id</td>
			
			<td>内容</td>
		
		<?php 
			while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
				# code...
		
		?>
		<tr>
			<td><?=$row['id'];?></td>
			<td><?=$row['name'];?></td>
			<td><a href="update.php?id=<?= $row['id']; ?>">更新</a></td>
			<td><a href="delete.php?id=<?= $row['id']; ?>">删除</a></td>
			
		</tr>
		<?php 
		}
		?>
		<td><a href="create.php">创建</a></td>
		</tr>
	</table>
</body>
</html>

更新权限

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
		<form action="update.php" method="GET">
		<input type="hidden" name="id" value="<?php echo $_GET['id']?>">
		更新内容:<input type="text" name="content">
		<input type="submit" value="搜索">
	</form>
</body>
</html>
<?php

$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
//$sql = "DELETE FROM `search` WHERE `id` = '$_POST[id]'";

//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,"UPDATE content set name='$_GET[content]' WHERE id ='$_GET[id]'");//运行sql
$sql="select * from content";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql

?>
<!--显示的效果-->
<!DOCTYPE html>
<?php
session_start();
if($_SESSION['login']!=1)
{
	echo "<script>alert('抱歉,用户还没登录');window.location.href='cyg1.php';</script>";
}
?>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<table border="1" cellpadding="5">
		<tr>
			<td>id</td>
			
			<td>内容</td>
		
		<?php 
			while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
				# code...
		
		?>
		<tr>
			<td><?=$row['id'];?></td>
			<td><?=$row['name'];?></td>
			<td><a href="update.php?id=<?= $row['id']; ?>">更新</a></td>
			<td><a href="delete.php?id=<?= $row['id']; ?>">删除</a></td>
			<td><a href="create.php">创建</a></td>
		</tr>
		<?php 
		}
		?>
		</tr>
	</table>
</body>
</html>

创建权限

<?php
session_start();
if($_SESSION['login']!=1)
{
	echo "<script>alert('抱歉,用户还没登录');window.location.href='cyg1.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="create.php" method="POST">
		 <textarea style="height:150px;width:200px;" name="content">
		
		 </textarea>
		
		<input type="submit" value="提交">
	</form>
</body>
</html>
<?php
if(!$_POST['content'])
{
exit();
}
$content=$_POST['content'];
$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql = "INSERT INTO content(name)
VALUES ('{$content}')";
 
mysqli_query($link,$sql);
echo "<script>alert('创建成功');location.href='index.php';</script>";
?>

求职者的权限(查看权限)

<?php
session_start();
if($_SESSION['login']!=1)
{
	echo "<script>alert('抱歉,用户还没登录');window.location.href='cyg1.php';</script>";
}
?>
<?php
$link=mysqli_connect('localhost','root','root','a');
//然后是指定php链接数据库的字符集
mysqli_set_charset($link,'utf8');
$sql="select name,id from content";
//模糊查询出像数据库中的title或者content里面的值或者说像数据库中的title或者content里面的某一段值相对应的就行了,就可以输出啦
$result=mysqli_query($link,$sql);//运行sql

?>
<!--显示的效果-->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<h1>求职者只能查看</h1>
	<table border="1" cellpadding="5">
		<tr>
			<td>id</td>
			
			<td>内容</td>
		
		<?php 
			while ($row=mysqli_fetch_array($result)) {//把对象编程数组输出,不然会报错哦
				# code...
		
		?>
		<tr>
			<td><?=$row['id'];?></td>
			<td><?=$row['name'];?></td>
		</tr>
		<?php 
		}
		?>
		</tr>
	</table>
	
</body>
</html>

sql

-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2022-10-07 23:15:43
-- 服务器版本: 5.7.26
-- PHP 版本: 7.3.4

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- 数据库: `a`
--

-- --------------------------------------------------------

--
-- 表的结构 `content`
--

CREATE TABLE `content` (
  `id` int(11) NOT NULL,
  `name` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 转存表中的数据 `content`
--

INSERT INTO `content` (`id`, `name`) VALUES
(2, '		\r\n		 HTML标签textarea在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定textarea的尺寸,但大小在浏览器中(firefox、chrome)还是可以调节的,使用CSS的height和width属性效果和rows cols定义的效果相同,可以拖动右下角图标改变大小。但是过分拖动大小会影响页面布局,使页面变得不美观。可以通过添加如下样式禁用拖动,固定大小:'),
(7, '		\r\n		  	HTML标签textarea在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定textarea的尺寸,但大小在浏览器中(firefox、chrome)还是可以调节的,使用CSS的height和width属性效果和rows cols定义的效果相同,可以拖动右下角图标改变大小。但是过分拖动大小会影响页面布局,使页面变得不美观。可以通过添加如下样式禁用拖动,固定大小: 	更新 	删除\r\n5 	'),
(8, '666');

-- --------------------------------------------------------

--
-- 表的结构 `qx`
--

CREATE TABLE `qx` (
  `id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '名字',
  `type` int(255) NOT NULL COMMENT '用户的类型(2管理员  员工1)',
  `time` date NOT NULL COMMENT '时间',
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- 转存表中的数据 `qx`
--

INSERT INTO `qx` (`id`, `name`, `type`, `time`, `password`) VALUES
(1, '陈业贵', 2, '2022-10-07', '123456'),
(2, 'liwen', 1, '2022-10-07', '123456'),
(3, 'aaa', 1, '2022-10-07', '123456'),
(4, 'kkk', 1, '2022-10-07', '123456');

--
-- 转储表的索引
--

--
-- 表的索引 `content`
--
ALTER TABLE `content`
  ADD PRIMARY KEY (`id`);

--
-- 表的索引 `qx`
--
ALTER TABLE `qx`
  ADD PRIMARY KEY (`id`);

--
-- 在导出的表使用AUTO_INCREMENT
--

--
-- 使用表AUTO_INCREMENT `content`
--
ALTER TABLE `content`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- 使用表AUTO_INCREMENT `qx`
--
ALTER TABLE `qx`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

boss账号:陈业贵 123456 求职者:aaa 123456 liwen 123456 kkk 123456

效果: