tp5的增刪改查

檢查所屬環境是否否和,

參考官方文檔安裝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>

至此簡單的增刪改查都已完成。

發佈了30 篇原創文章 · 獲贊 17 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章