tp5的增删改查

http://blog.csdn.net/han_cui/article/details/54947972

首先打开官网参考手册http://www.kancloud.cn/manual/thinkphp5/118003

检查所属环境是否否和,

参考官方文档安装TinkPHP5.0(三种方法)

并查看目录结构

配置一域名直接指向public,即可访问其下面的index.php入口文件


然后看其应用配置文件



配置文件基本不必更改,(若必要可更改访问默认模块)。

看其数据库配置文件

填写必要的东西


接下来创建Model,view等文件


相对于thinkphp3.2来说改变有所大,控制器名不能与模型名必须不同

以下是其简单代码:

Index.php控制器:

<?php


namespace app\index\controller;


use think\Controller;
use think\Request;
use app\index\model\Goods;


class Index extends Controller
{
    public function index()
    {
        return view('goods');
    }
    public function insert()
    {
        $request = Request::instance();
        $data = $request->post();
        $goods = new Goods;
        $result = $goods->insertData($data);
        if ($result) {
            $this->success('新增成功', 'index/show');
        } else {
            $this->error('新增失败');
        }
        
    }
    //展示
    public function show()
    {
        $goods = new Goods;
        $arr = $goods->show();
        return $this->fetch('show',['arr' => $arr]);
    }
    //删除
    public function delete()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $goods = new Goods;
        $result = $goods->deleteData($id);
        if ($result) {
            $this->success('删除成功', 'index/show');
        } else {
            $this->error('删除失败');
        }
    }
    //修改页面
    public function update()
    {
        $request = Request::instance();
        $id = $request->get('id');
        $goods = new Goods;
        $res = $goods->findData($id);
        return view('update',['res' =>$res]);
    }
    //修改数据
    public function save()
    {
        $id = $_POST['u_id'];
        $request = Request::instance();
        $data = $request->post();
        // var_dump($data);die;
        $goods = new Goods;
        $result = $goods->updateData($data,$id);
        if ($result) {
            $this->success('修改成功', 'index/show');
        } else {
            $this->error('修改失败');
        }
    }
}

模型Goods.php:

<?php
namespace app\index\model;


use think\Db;
use think\Model;


class Goods extends Model
{
protected $table = 'goods';//表名


//增加
function insertData($data)
{
return Db::table($this->table)->insertGetId($data);
}
//展示
function show()
{
return Db::table($this->table)->select();
}
//删除
function deleteData($id)
{
return Db::table($this->table)->where('u_id','=',$id)->delete();
}
//查询单条
function findData($id)
{
return Db::table($this->table)->where('u_id','=',$id)->find();
}
//修改
function updateData($data,$id)
{
return Db::table($this->table)->where('u_id','=',$id)->update($data);
}
}

基本的表单页面goods.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<form action="insert" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="u_name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="u_pwd"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</center>
</body>
</html>

展示页面show.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<table border=1>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
{volist name="arr" id="vo"}
<tr>
<td>{$vo.u_id}</td>
<td>{$vo.u_name}</td>
<td>{$vo.u_pwd}</td>
<td><a href="update?id={$vo.u_id}">修改</a><a href="delete?id={$vo.u_id}">删除</a></td>
</tr>
{/volist}
</table>
</center>
</body>
</html>

修改页面update.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<form action="save" method="post">
<table>
<tr>
<td></td>
<td><input type="text" name="u_id" value="{$res['u_id']}"></td>
</tr>
<tr>
<td>用户名</td>
<td><input type="text" name="u_name" value="{$res['u_name']}"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="u_pwd" value="{$res['u_pwd']}"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="修改"></td>
</tr>
</table>
</form>
</center>
</body>
</html>

至此简单的增删改查都已完成。


猜你喜欢

转载自blog.csdn.net/alashan007/article/details/78216030