CURD操作

CURD

CURD代表创建(Create) 、 更新(Update) 、 读取(Read) 和 删除(Delete) 操作。

创建数据

创建一个add.html 模板文件,内容为:
<FORM method="post" action="__URL__/insert">
学号:<INPUT type="text" name="no"><br/>
姓名:<INPUT type="text" name="name"><br/>
<INPUT type="submit" value="提交">

</FORM>

创建一个FormController.class.php文件,定义FormController类

<?php

class FormController {

    public function insert(){
    $Form = D('Form');
    if($Form->create()) {
    $result = $Form->add();
        if($result) {
            $this->success('数据添加成功!');
        }else{
             $this->error('数据添加错误!');
}
  }else{
       $this->error($Form->getError());
  }
    }

}

 读取数据

扫描二维码关注公众号,回复: 1439986 查看本文章

public function read($id=0){
$Form = M('Form');
// 读取数据
$data = $Form->find($id);
if($data) {
$this->assign('data',$data);// 模板变量赋值
}else{
$this->error('数据错误');
}
$this->display();

}

更新数据

创建edit.html模板:

<FORM method="post" action="__URL__/insert">
学号:<INPUT type="text" name="no"><br/>
姓名:<INPUT type="text" name="name"><br/>
<INPUT type="submit" value="提交">

</FORM>


方法:

public function edit($id=0){
$Form = M('Form');
$this->assign('vo',$Form->find($id));
$this->display();
}
public function update(){
$Form = D('Form');
if($Form->create()) {
$result = $Form->save();
if($result) {
$this->success('操作成功!');
}else{
$this->error('写入错误!');
}
}else{
$this->error($Form->getError());
}

}

删除数据

$Form = M('Form');
$Form->delete(5);

猜你喜欢

转载自blog.csdn.net/spp_ty/article/details/79689030