更新数据

更新数据

在成功写入并读取数据之后,我们就可以对数据进行编辑操作了,首先我们添加一个编辑表单的模板文件edit.html,如下:

Home/View/Form/edit.html

<FORM method="post" action="__URL__/update">
标题:<INPUT type="text" name="title" value="{$vo.title}"><br/>
内容:<TEXTAREA name="content" rows="5" cols="45">{$vo.content}</TEXTAREA><br/>
<INPUT type="hidden" name="id" value="{$vo.id}">
<INPUT type="submit" value="提交">
</FORM>

编辑模板不同于新增表单,需要对模板进行变量赋值,所以,我们这次需要在FormController类添加两个操作方法:

Home/Controller/FormController.class.php

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());
    }
 }

完成后,我们就可以访问
http://localhost/app/index.php/home/Form/edit/id/1

结果:

清风明月本无价,近水远山皆有情。
原文地址:https://www.cnblogs.com/onlylove2015/p/5151165.html