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('添加成功','all',5); }else{ $this->error('添加失敗'); } } //讀取操作(read) public function all(){ $newsModel=M('news'); //$newsModel->select(): select * from news $data=$newsModel->select(); //assign(): 分配數(shù)據(jù)(相當(dāng)于賦值操作) $this->assign('news',$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('修改成功','all',3); }else{ $this->error('修改失敗'); } }else{ /** * I():Thinkphp中簡單的獲取參數(shù)的方式 * I(“post.id”): $_POST[‘id’]; * I(“get.id”): $_GET[‘id’]; * param變量類型是框架特有的支持自動判斷當(dāng)前請求類型的變量獲取方式 **/ $id = I('id'); //等同于I( 'param.id') $newsModel = M("news"); //find($id):讀取id = $id 的新聞數(shù)據(jù) $data = $newsModel->find($id); $this->assign('news',$data); $this->display(); } } //刪除操作(delete) public function del(){ $newsModel = M('news'); $id = I('id'); if(isset($id) && $newsModel->delete($id)){ $this->success('刪除成功'); }else{ $this->error('刪除失敗'); } } } ?>
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)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
