基于tp5的一个简单的增删改查demo

1.html部分

01.user.html

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
.layui-laypage-curr em {margin-right: 10px;font-style: normal;}
a{margin-right: 10px;}
</style>
<body>
<h4>用户信息管理</h4>
<a href="{:url('index/add_user')}">添加</a>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>电话</td>
<td>操作</td>
</tr>
{volist name="data_list" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{if condition="$vo.sex eq '1'"}男{elseif condition="$vo.sex eq '2'"}女{else /}保密{/if}</td>
<td>{$vo.age}</td>
<td>{$vo.tel}</td>
<td><a href="{:url('index/edit_user',['id' => $vo['id']])}">编辑</a> | <a href="{:url('index/del',['id' => $vo['id']])}">删除</a></td>
</tr>
{/volist}


</table>
<div>{$page}</div>


</body>
</html>

02.add_user.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="{:url('index/do_add_user')}" method="post">
姓名 :<input type="text" name="name" value=""><br><br>
性别 :男<input type="radio" name="sex" value="1" checked="checked">
   女<input type="radio" name="sex" value="2">
   保密<input type="radio" name="sex" value="0"><br><br>
年龄 :<input type="text" name="age" value=""><br><br>
电话 :<input type="text" name="tel" value=""><br><br>
<input type="submit" value="确定">
<input type="button" value="重置">
</form>
</body>
</html>
03.edit_html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="{:url('index/do_edit_user')}" method="post">
姓名 :<input type="text" name="name" value="{$info.name}"><br><br>
性别 :
{if condition="$info.sex eq '1'"}
男<input type="radio" name="sex" value="1" checked="checked">
女<input type="radio" name="sex" value="2">
保密<input type="radio" name="sex" value="0"><br><br>
{elseif condition="$info.sex eq '2'"}
男<input type="radio" name="sex" value="1">
女<input type="radio" name="sex" value="2" checked="checked">
保密<input type="radio" name="sex" value="0"><br><br>
{else /}
男<input type="radio" name="sex" value="1">
女<input type="radio" name="sex" value="2">
保密<input type="radio" name="sex" value="0" checked="checked"><br><br>
{/if}
年龄 :<input type="text" name="age" value="{$info.age}"><br><br>
电话 :<input type="text" name="tel" value="{$info.tel}"><br><br>
<input type="hidden" value="{$info['id']}" name="id">
<input type="submit" value="确定">
<input type="button" value="取消">
</form>
</body>
</html>

2.控制器部分Index.php

<?php
namespace app\index\controller;
use think\Controller;
use think\Model;
use think\Db;
header("Content-type:text/html;charset=UTF-8");
class Index extends Controller
{
    public function index()
    {
        $result = Db::table('user')->paginate(5);
// 获取分页显示
$page = $result->render();
// dump($result);die;
// 模板变量赋值
$this->assign('data_list', $result);
$this->assign('page', $page);
return $this->fetch('user');
        // return view('user',['data_list'=>$result]);
    }
    public function add_user()
    {
    return view('add_user');
    }
    public function do_add_user()
    {
    if(request()->isPost()){
    $data = input('post.');
    $add_res = Db::name('user')->insert($data);
    if($add_res){
    $this->success('新增成功', 'Index/index');
    // return $this->success('新增成功');
    }else{
    return $this->error('添加失败!');
    }
    }
    }
    public function edit_user(){
    if(request()->isGet()){
    $id = input('id');
    $info = Db::table('user')->where('id',$id)->find();
    // dump($info);die;
    return $this->fetch('edit_user',array('info'=>$info));
    }
    }
    public function do_edit_user()
    {
    if(request()->isPost()){
    $data['name'] = input('name');
    $data['sex'] = input('sex');
    $data['age'] = input('age');
    $data['tel'] = input('tel');
    $res = Db::table('user')->where('id',input('post.id'))->update($data);
    // echo db('user')->getlastsql();die;
    if($res){
    $this->success('更新成功','index');
    }else{
    $this->error('更新失败!');
    }
    }
    }
    public function del(){
    if(request()->isGet()){
    $del_id = input('id');
    $res = Db::table('user')->where('id',$del_id)->delete();
    if($res){
    $this->success('删除成功!','index');
    }else{
    $this->error('删除失败!');
    }
    }
    }
}
3.sql文件部分
/*
Navicat MySQL Data Transfer


Source Server         : 127.0.0.1
Source Server Version : 50547
Source Host           : 127.0.0.1:3306
Source Database       : test


Target Server Type    : MYSQL
Target Server Version : 50547
File Encoding         : 65001


Date: 2018-03-30 16:28:50
*/


SET FOREIGN_KEY_CHECKS=0;


-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  `age` int(2) DEFAULT NULL,
  `tel` varchar(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

4.效果

猜你喜欢

转载自blog.csdn.net/lfbin5566/article/details/79759551