入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

ThinkPHP5.0基础增删改查

创建时间:2017-02-09 投稿人: 浏览次数:120

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

检查所属环境是否否和,

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

并查看目录结构

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


然后看其应用配置文件



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

看其数据库配置文件

填写必要的东西


接下来创建Model,view等文件


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

以下是其简单代码:

Index.php控制器:

<?php


namespace appindexcontroller;


use thinkController;
use thinkRequest;
use appindexmodelGoods;


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 appindexmodel;


use thinkDb;
use thinkModel;


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>

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

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。