通过一个页面实现增删改查的功能:
<?php // 类型 class xm { private $ser; private $unam; private $pad; private $dbname; private $conn; function __construct($ser = 'localhost', $unam = 'root', $pad = 'root', $dbname = 'aproj') { $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 index() { // 查询 $conn = $this->conn; $sql = "select * from proj order by ctime desc"; $res = $conn->query($sql); if (!$res) { return $conn->error; } else { return $res->fetch_all(MYSQLI_ASSOC); } } function query($name, $url, $ctime, $isadd = 0) { // 增 删除 修改 $conn = $this->conn; if ($isadd == 0) { $sql = "insert into proj (name,url,ctime) values('$name','$url','$ctime')"; } else { $sql = "update proj set name='$name', url='$url' where id = '$isadd' "; } $res = $conn->query($sql); if ($res) { return $res; } else { return $conn->error; } } function del($id) { // 增 删除 修改 $conn = $this->conn; $sql = "delete from proj where id = '$id'"; $res = $conn->query($sql); if ($res) { return $res; } else { return $conn->error; } } function __destruct() { $conn = $this->conn; $conn->close(); } } $xm = new xm(); $ction = 'index'; if (isset($_GET['c'])) { $ction = $_GET['c']; } switch ($ction) { case 'query': if ($_POST) { $id = $_POST['id']; $name = trim($_POST['name']); $url = trim($_POST['url']); $ctime = time(); if ($id == 0) { $xm->query($name, $url, $ctime); } else { $xm->query($name, $url, $ctime, $id); } // header('location:index.php'); } break; case 'del': if (isset($_GET['id'])) { $id = $_GET['id']; $xm->del($id); // header('location:index.php'); } break; } // 查询所有数据 $arr = $xm->index(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>所有项目说明</title> <link rel="stylesheet" href="js/css/layui.css" media="all"> </head> <body> <div style="width:800px; float:left; padding-left: 50px;"> <table class="layui-table" lay-filter="test" id="demo"> <colgroup> <col width="10"> <col width="200"> <col width="150"> <col width="200"> <col width="200"> <col> </colgroup> <thead> <tr> <th>ID</th> <th>项目名</th> <th>位置</th> <th>创建时间</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($arr as $val) { ?> <tr> <td><?php echo $val['id']; ?></td> <td><?php echo $val['name']; ?></td> <td><?php echo $val['url']; ?></td> <td><?php echo date('Y-m-d', $val['ctime']); ?></td> <td> <a class="layui-btn layui-btn-xs edit" id="<?php echo $val['id'] ?>">编辑</a> <a class="layui-btn layui-btn-danger layui-btn-xs" href="index.php?c=del&id=<?php echo $val['id'] ?>">删除</a> </td> </tr> <?php } ?> </tbody> </table> </div> <div style="width: 300px; float:left; padding-top: 50px;"> <form class="layui-form" action="index.php?c=query" method="post"> <div class="layui-form-item"> <label class="layui-form-label">项目名:</label> <div class="layui-input-block"> <input type="text" name="name" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">文件位置:</label> <div class="layui-input-block"> <input type="text" name="url" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">文件位置:</label> <div class="layui-input-block"> <input type="text" name="id" id="ids" value='0' required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button> <button type="reset" class="layui-btn layui-btn-primary">重置</button> </div> </div> </form> </div> </body> <script src="js/layui.all.js"></script> <script src="js/jquery.min.js"></script> <script> $('.edit').click(function(){ var id = $(this).attr('id'); $('#ids').val(id); }); </script> </html>
来源:https://www.cnblogs.com/ziyandeyanhuo/p/12572044.html