ThinkPHP示例:CURD

本文详细介绍了ThinkPHP框架中控制器的基本操作,包括数据的查询、写入、更新、删除及编辑等核心功能,通过具体代码示例展示了如何使用Model类进行数据库操作。

完整的控制器文件:

  1. class IndexAction extends Action {
  2.     // 查询数据
  3.     public function index() {
  4.         $Form = M("Form");
  5.         $list = $Form->limit(3)->order('id desc')->select();
  6.         $this->list =  $list;
  7.         $this->display();
  8.     }
  9.     // 写入数据
  10.     public function insert() {
  11.         $Form = D("Form");
  12.         if ($vo = $Form->create()) {
  13.             $list = $Form->add();
  14.             if ($list !== false) {
  15.                 $this->success('数据保存成功!',U('Index/index'));
  16.             } else {
  17.                 $this->error('数据写入错误!');
  18.             }
  19.         } else {
  20.             $this->error($Form->getError());
  21.         }
  22.     }
  23.     // 更新数据
  24.     public function update() {
  25.         $Form = D("Form");
  26.         if ($vo = $Form->create()) {
  27.             $list = $Form->save();
  28.             if ($list !== false) {
  29.                 $this->success('数据更新成功!',U('Index/index'));
  30.             } else {
  31.                 $this->error("没有更新任何数据!");
  32.             }
  33.         } else {
  34.             $this->error($Form->getError());
  35.         }
  36.     }
  37.     // 删除数据
  38.     public function delete($id) {
  39.         if (!empty($id)) {
  40.             $Form = M("Form");
  41.             $result = $Form->delete($id);
  42.             if (false !== $result) {
  43.                 $this->success('删除成功!');
  44.             } else {
  45.                 $this->error('删除出错!');
  46.             }
  47.         } else {
  48.             $this->error('ID错误!');
  49.         }
  50.     }
  51.     // 编辑数据
  52.     public function edit($id) {
  53.         if (!empty($id)) {
  54.             $Form = M("Form");
  55.             $vo = $Form->getById($id);
  56.             if ($vo) {
  57.                 $this->vo   =   $vo;
  58.                 $this->display();
  59.             } else {
  60.                 $this->error('数据不存在!');
  61.             }
  62.         } else {
  63.             $this->error('数据不存在!');
  64.         }
  65.     }
  66. }

转载于:https://www.cnblogs.com/shanmao/p/3227316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值