码迷,mamicode.com
首页 > Web开发 > 详细

thinkphp crud实例代码

时间:2017-01-30 17:07:29      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:upd   bsp   display   date()   实例代码   删除   存在   更新   select   

class IndexAction extends Action {
    // 查询数据
    public function index() {
        $Form = M("Form");
        $list = $Form->limit(3)->order(‘id desc‘)->select();
        $this->list =  $list;
        $this->display();
    }
    // 写入数据
    public function insert() {
        $Form = D("Form");
        if ($vo = $Form->create()) {
            $list = $Form->add();
            if ($list !== false) {
                $this->success(‘数据保存成功!‘,U(‘Index/index‘));
            } else {
                $this->error(‘数据写入错误!‘);
            }
        } else {
            $this->error($Form->getError());
        }
    }
    // 更新数据
    public function update() {
        $Form = D("Form");
        if ($vo = $Form->create()) {
            $list = $Form->save();
            if ($list !== false) {
                $this->success(‘数据更新成功!‘,U(‘Index/index‘));
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($Form->getError());
        }
    }
    // 删除数据
    public function delete($id) {
        if (!empty($id)) {
            $Form = M("Form");
            $result = $Form->delete($id);
            if (false !== $result) {
                $this->success(‘删除成功!‘);
            } else {
                $this->error(‘删除出错!‘);
            }
        } else {
            $this->error(‘ID错误!‘);
        }
    }
    // 编辑数据
    public function edit($id) {
        if (!empty($id)) {
            $Form = M("Form");
            $vo = $Form->getById($id);
            if ($vo) {
                $this->vo   =   $vo;
                $this->display();
            } else {
                $this->error(‘数据不存在!‘);
            }
        } else {
            $this->error(‘数据不存在!‘);
        }
    }
 }

 

thinkphp crud实例代码

标签:upd   bsp   display   date()   实例代码   删除   存在   更新   select   

原文地址:http://www.cnblogs.com/wordblog/p/6358168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!