Home php教程 PHP开发 [ThinkPHP Series] CURD operation of Thinkphp framework (3)

[ThinkPHP Series] CURD operation of Thinkphp framework (3)

Dec 28, 2016 am 10:38 AM

a) What is CURD operation?
C (create): create; U (update): update; R (read): read; D (delete): delete

NewsController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller{
    //添加操作
    public function add(){
        //实例化news模型,news为数据库中的news表 
        $newsModel=M("news"); 
        //create() :根据表单提交的POST数据创建数据对象
        $data = $newsModel->create();
        //add() :写入数据到news数据库中
        if($newsModel->add($data)){
        /**
         * success() 和error()的有三个参数
         * 第一个参数表示提示信息
         * 第二个参数表示跳转地址
         * 第三个参数是跳转时间(单位为秒)
         * */
            $this->success(&#39;添加成功&#39;,&#39;all&#39;,5);
        }else{
            $this->error(&#39;添加失败&#39;);
        }
    }
    //读取操作(read)
    public function all(){
        $newsModel=M(&#39;news&#39;);
        //$newsModel->select(): select * from news
        $data=$newsModel->select();
        //assign(): 分配数据(相当于赋值操作)
        $this->assign(&#39;news&#39;,$data);
        $this->display();
    }
    /**
    * 更新操作
    * 注意:在对应的静态页中对应的form表单中要添加一句
    * <input type=”hidden”name=”id”value=”{$news.id}”>
    * 其中name的值为news表的主键,value的值为控制器中传过来的值
    * 是数据库中对应的主键。
    **/
    public function edit(){
        if(IS_POST){
            $newsModel=M("news");
            $data=newsModel->create();
            //save() :根据条件更新记录
            if($newsModel->save($data)){
                $this->success(&#39;修改成功&#39;,&#39;all&#39;,3);
            }else{
                $this->error(&#39;修改失败&#39;);
            }
        }else{
    /**
    * I():Thinkphp中简单的获取参数的方式
    * I(“post.id”): $_POST[‘id’];
    * I(“get.id”): $_GET[‘id’];
    * param变量类型是框架特有的支持自动判断当前请求类型的变量获取方式
    **/
        $id = I(&#39;id&#39;); //等同于I( &#39;param.id&#39;)
        $newsModel = M("news");
        //find($id):读取id = $id 的新闻数据
            $data = $newsModel->find($id);
            $this->assign(&#39;news&#39;,$data);
            $this->display();
        }
    }
    //删除操作(delete)
    public function del(){
        $newsModel = M(&#39;news&#39;);
        $id = I(&#39;id&#39;);
        if(isset($id) && $newsModel->delete($id)){
            $this->success(&#39;删除成功&#39;);
        }else{
            $this->error(&#39;删除失败&#39;);
        }
    }
}
?>
Copy after login

Okay, the simple basic CURD operations of ThinkPHP are introduced here. For more detailed usage, please see the official online documentation:

a) Version 3.1 manual: http://doc.thinkphp.cn/manual.html
b) Version 3.2 manual: http ://document.thinkphp.cn/manual_3_2.html

The above is the content of [ThinkPHP series] CURD operation of Thinkphp framework (3). For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24