国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) [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為數(shù)據(jù)庫中的news表 
        $newsModel=M("news"); 
        //create() :根據(jù)表單提交的POST數(shù)據(jù)創(chuàng)建數(shù)據(jù)對象
        $data = $newsModel->create();
        //add() :寫入數(shù)據(jù)到news數(shù)據(jù)庫中
        if($newsModel->add($data)){
        /**
         * success() 和error()的有三個參數(shù)
         * 第一個參數(shù)表示提示信息
         * 第二個參數(shù)表示跳轉(zhuǎn)地址
         * 第三個參數(shù)是跳轉(zhuǎn)時間(單位為秒)
         * */
            $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(): 分配數(shù)據(jù)(相當(dāng)于賦值操作)
        $this->assign(&#39;news&#39;,$data);
        $this->display();
    }
    /**
    * 更新操作
    * 注意:在對應(yīng)的靜態(tài)頁中對應(yīng)的form表單中要添加一句
    * <input type=”hidden”name=”id”value=”{$news.id}”>
    * 其中name的值為news表的主鍵,value的值為控制器中傳過來的值
    * 是數(shù)據(jù)庫中對應(yīng)的主鍵。
    **/
    public function edit(){
        if(IS_POST){
            $newsModel=M("news");
            $data=newsModel->create();
            //save() :根據(jù)條件更新記錄
            if($newsModel->save($data)){
                $this->success(&#39;修改成功&#39;,&#39;all&#39;,3);
            }else{
                $this->error(&#39;修改失敗&#39;);
            }
        }else{
    /**
    * I():Thinkphp中簡單的獲取參數(shù)的方式
    * I(“post.id”): $_POST[‘id’];
    * I(“get.id”): $_GET[‘id’];
    * param變量類型是框架特有的支持自動判斷當(dāng)前請求類型的變量獲取方式
    **/
        $id = I(&#39;id&#39;); //等同于I( &#39;param.id&#39;)
        $newsModel = M("news");
        //find($id):讀取id = $id 的新聞數(shù)據(jù)
            $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;);
        }
    }
}
?>

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)