• ThinkPHP示例:CURD


    完整的控制器文件:

    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. }
  • 相关阅读:
    python--进程
    python---多线程
    python--上下文管理器
    python中的单例模式
    装饰器
    匿名函数
    python的内置方法
    命名元组
    如何管理我们的项目环境
    启动APP遇到“UiAutomator exited unexpectedly with code 0, signal null”解决
  • 原文地址:https://www.cnblogs.com/shanmao/p/3227316.html
Copyright © 2020-2023  润新知