1 前台,创建一个数据库类,可以在前台页面进行引用
引用方法为
include_once './pmysql.php';
如果前台页面油循环的内容,需要用foreach来进行循环,
<?php
// 数据库类
class db{
private $ser;
private $unam;
private $pad;
private $dbname;
private $conn;
function __construct($ser='localhost',$unam='root',$pad='root',$dbname='wzzh')
{
$this->ser = $ser;
$this->unam = $unam;
$this->pad = $pad;
$this->dbname = $dbname;
// 连接数据库
$conn = new mysqli($ser,$unam,$pad,$dbname);
if($conn->connect_error){
die($conn->error);
}else{
$this->conn = $conn;
}
}
function query($str){
// 增 删除 修改
$conn = $this->conn;
$res = $conn->query($str);
if($res){
return $res;
}else{
return $conn->error;
}
}
function select($str){
// 查询
$conn = $this->conn;
$res = $conn->query($str);
if(!$res){
return $conn->error;
}else{
return $res->fetch_all(MYSQLI_ASSOC);
}
}
function __destruct()
{
$conn = $this->conn;
$conn->close();
}
}
$db = new db();
2 关于后台,首先要做一个登录注册界面,界面实现的内容为增删改查,左侧导航栏需要添加前台对接页面出现的内容,基本写法如下
<dl class="layui-nav-child">
<dd><a href="wed.php">计划规划</a></dd>
<dd><a href="addwed.php">新增规划</a></dd>
</dl>,连接的分别是表单以及增删页面,
进行编辑的基本写法需要穿出ID的值,
<a href="editfuwu.php?id=<?php echo $val['id'] ?>">编辑</a>
<a href="javascript:;" class="del" tt="<?php echo $val['id'] ?>">删除</a>
删除都需要ajax进行操作
$('.del').click(function(){
var t = confirm('确认删除吗?');
var id = $(this).attr('tt');
if(t){
$.ajax({
url: 'delfuwu.php',
data: {id,id},
dataType: 'json',
type: 'post',
success: function(res){
//返回结果进行操作
if(res.code == 1){
// location.href = 'index.php';
location.reload();
}else{
alert('删除失败,稍后再试');
}
}
});
}
});