💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
这两天帮别人做了一个增删改查,我想着记录下来也好: 目录结构![](https://box.kancloud.cn/6f626d2b884788269cfe44be6be5feff_294x762.png) 在thinkphp/library/think/View.php文件里修改模板路径然后写到页面头文件中 ![](https://box.kancloud.cn/ee934d6fa1c8cbd12f2e4b6f14caef13_669x185.png) 在database.php修改数据库: ![](https://box.kancloud.cn/4fbefd66976a9cb72dd5aecb1c07c13d_517x847.png) 下面贴出页面代码: add.html: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加学生信息</title> <link rel="stylesheet" href="__ADMIN__/layui/css/layui.css"> <script src="__ADMIN__/layui/layui.all.js"></script> <script> ;!function () { //无需再执行layui.use()方法加载模块,直接使用即可 var form = layui.form , layer = layui.layer; //… }(); </script> </head> <body> <form method="post" action="{:url('add')}"> <div class="layui-form-item"> <label class="layui-form-label"></label> <div class="layui-input-inline"> <input type="hidden" name="id"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">学号框</label> <div class="layui-input-inline"> <input type="text" name="number" required lay-verify="required" placeholder="请输入学号" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">姓名框</label> <div class="layui-input-inline"> <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">性别框</label> <div class="layui-input-inline"> <label class="layui-btn layui-btn-warm"><input type="radio" name="sex" value="男">男</label> <label class="layui-btn layui-btn-warm"><input type="radio" name="sex" value="女">女</label> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">年龄框</label> <div class="layui-input-inline"> <input type="text" name="banji" required lay-verify="required" placeholder="请输入性别" class="layui-input"> </div> </div> <!--<div class="layui-form-item">--> <!--<label class="layui-form-label">年龄框</label>--> <!--<div class="layui-input-inline">--> <!--<input type="text" name="nianl" required lay-verify="required" placeholder="请输入班级" class="layui-input">--> <!--</div>--> <!--</div>--> <div class="layui-form-item"> <label class="layui-form-label">地址框</label> <div class="layui-input-inline"> <input type="text" name="frmali" required lay-verify="required" placeholder="请输入家庭住址" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">联系框</label> <div class="layui-input-inline"> <input type="text" name="phone" required lay-verify="required" placeholder="请输入联系方式" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button> </div> </div> </form> </body> </html> ~~~ edit.html: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>学生信息修改</title> <link rel="stylesheet" href="__ADMIN__/layui/css/layui.css"> <script src="__ADMIN__/layui/layui.all.js"></script> <script> ;!function () { //无需再执行layui.use()方法加载模块,直接使用即可 var form = layui.form , layer = layui.layer; //… }(); </script> </head> <body> <form method="post" action="{:url('update')}"> <div class="layui-form-item"> <label class="layui-form-label"></label> <div class="layui-input-inline"> <input type="hidden" value="{$student.id}" name="id"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">学号框</label> <div class="layui-input-inline"> <input type="text" name="number" value="{$student.number}" required lay-verify="required" placeholder="请输入学号" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">姓名框</label> <div class="layui-input-inline"> <input type="text" name="name" value="{$student.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-inline"> <label class="layui-btn layui-btn-warm"><input type="radio" name="sex" value="男">男</label> <label class="layui-btn layui-btn-warm"><input type="radio" name="sex" value="女">女</label> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">年龄框</label> <div class="layui-input-inline"> <input type="text" name="banji" value="{$student.banji}" 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-inline"> <input type="text" name="frmali" value="{$student.frmali}" 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-inline"> <input type="text" name="phone" value="{$student.phone}" required lay-verify="required" placeholder="请输入联系方式" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button type="submit" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button> </div> </div> </form> </body> </html> ~~~ index.html: ~~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <link rel="stylesheet" href="__ADMIN__/layui/css/layui.css"> <script src="__ADMIN__/layui/layui.all.js"></script> <script> ;!function () { //无需再执行layui.use()方法加载模块,直接使用即可 var form = layui.form , layer = layui.layer; //… }(); </script> <title>学生管理系统</title> </head> <body> <table class="layui-table"> <colgroup> <col width="150"> <col width="200"> <col> </colgroup> <thead> <tr> <th>序号</th> <th>学号</th> <th>姓名</th> <th>性别</th> <th>班级</th> <th>家庭住址</th> <th>联系方式</th> <th>添加时间</th> <th>数据操作</th> </tr> </thead> <tbody> {volist name="list" id="data" key="key"} <tr> <td>{$key}</td> <td>{$data.number}</td> <td>{$data.name}</td> <td>{$data.sex}</td> <td>{$data.banji}</td> <td>{$data.frmali}</td> <td>{$data.phone}</td> <td>{$data.time}</td> <td><a class="layui-btn layui-btn-radius layui-btn-danger" href="{:url('delete?id=' . $data->getData('id'))}"><i class="glyphicon glyphicon-trash"></i>&nbsp;删除</a><a class="layui-btn layui-btn-radius" href="{:url('edit?id=' . $data->getData('id'))}"><i class="glyphicon glyphicon-trash"></i>&nbsp;编辑</a></td> </tr> {/volist} </tbody> </table> <a href="{:url('tianjia')}" class="layui-btn layui-btn-normal">添加学生信息</a> </body> </html> ~~~ 控制器代码 Index.php: ~~~ <?php namespace app\index\controller; use think\Controller; use think\Db; use think\Request; use app\index\controller\User as UserModel; use think\Validate; class Index extends Controller { /** * @return mixed * @throws \think\exception\DbException * 首页模板数据 */ public function index() { // 获取用户列表 $list = UserModel::all(); // $info = Db::table('user'); // $user = $info->where('id',1)->select(); // $data[''] = $user; // 模板变量赋值 $this->assign('list',$list); // dump($data); // $this->assign('data', data($user)); // 模板渲染 return $this->fetch('index'); } /** * @param string $id * @throws \think\exception\DbException * 删除数据 */ public function delete($id = ''){ // $id = $request->param('id'); // 读取用户id数据 $student = UserModel::get($id); // 删除数据 if ($student->delete()){ $this->success('删除成功'); }else{ $this->error('删除失败',$student->getError()); } } /** * @param string $id * @return mixed * @throws \think\exception\DbException * 渲染添加数据模板 */ public function edit($id = ''){ // 读取数据 $student = UserModel::get($id); // 模板变量赋值 $this->assign('student',$student); return $this->fetch('edit'); } /** * @param Request $request * @throws \think\exception\DbException * 更新数据方法名必须为update */ public function update(Request $request){ // 获取数据修改数据页面的id $id = $request->post('id'); // 读取数据 $user = UserModel::get($id); // dump($id); // $user->number = $request->post('number'); // if (false !== $user->save()) { // return '修改成功'; // 获取表单数据 $data = input('post.'); $user->number = $data['number']; $user->name = $data['name']; $user->sex = $data['sex']; $user->banji = $data['banji']; $user->frmali = $data['frmali']; $user->phone = $data['phone']; // 修改数据使用validate验证必须在save()加入$user->getData();才能使用验证器 if (false !== $user->validate(true)->save($user->getData())){ $this->success('修改成功',url('index/index/index')); }else{ $this->error($user->getError()); } } /** * 添加数据方法名必须要add */ public function add(){ $data = input('post.'); $user = new User; $user->number = $data['number']; $user->name = $data['name']; $user->sex = $data['sex']; $user->banji = $data['banji']; $user->frmali = $data['frmali']; $user->phone = $data['phone']; // $validate = new Index; if ($user->validate(true)->save($user->getData())){ return $this->success('添加数据成功',url('index/index/index')); }else{ $this->error($user->getError()); } } public function tianjia(){ return $this->fetch('add'); } } ~~~ User.php: ~~~ <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/6/9 * Time: 23:01 */ namespace app\index\controller; use think\Model; class User extends Model{ protected $table = 'user'; } ~~~ 验证器代码: User.php: ~~~ <?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/6/11 * Time: 14:49 */ namespace app\common\validate; use think\Validate; class User extends Validate{ protected $rule = [ 'number' => 'require|number|length:10', 'name' => 'require|length:2,8', 'sex' => 'require', 'banji' => 'require|length:2,3|number', 'frmali' => 'require|min:5|number', 'phone' => 'require|regex:1[3-8]{1}[0-9]{9}', ]; } ~~~ 数据库代码: xsxt.sql: # Host: localhost (Version: 5.5.53) # Date: 2019-06-11 20:58:34 # Generator: MySQL-Front 5.3 (Build 4.234) /*!40101 SET NAMES utf8 */; # # Structure for table "user" # CREATE TABLE `user` ( `id` int(25) NOT NULL AUTO_INCREMENT, `number` int(11) NOT NULL, `name` text NOT NULL, `sex` text NOT NULL, `banji` text NOT NULL, `frmali` text NOT NULL, `phone` text NOT NULL, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; # # Data for table "user" # INSERT INTO `user` VALUES (5,2019061211,'123','12','12','123456','17876112564','2019-06-11 19:59:28'),(6,2019051111,'test','男性','11','12345','15654542365','2019-06-11 20:00:19'),(7,2019061231,'admin','女','00','12345','18345678910','2019-06-11 20:21:52'),(8,2019061234,'1234','男','00','12345','17123456789','2019-06-11 20:20:19'); 还有一个就是这次用到了前端框架layui