表单不更新 cakephp 代码中的数据库表?

菜鸟

大家好,我遇到了这个问题,当我单击提交时,表单没有更新。它进入 if($this->request->is("post")){....} 块,但它没有更新。

这是代码。

页面控制器.php

public function admin_edit($id=NULL){
    $this->request->data = $this->Page->find("first",array("conditions"=>array("Page.id"=>$id)));
    if($this->request->is('post')){
        $this->request->data["Page"]["id"] = $id;
        if($this->Page->save($this->data)){
            echo "Done";
        }


    }
}

admin_edit.php

URL -> http://localhost/cakephp_practice/admin/pages/edit/4

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="DataTable">
        <tr>
            <th>Administrator &gt; Edit Page</th>
        </tr>
        <tr>
            <td>

                <?php
                echo $this->Form->create('Page', array("action" => "edit", "method" => "Post",'enctype' => 'multipart/form-data', 'id' => 'editPage'));
                ?>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td colspan="2"> <span class="require">* Please note that all fields that have an asterisk (*) are required. </span></td>
                    </tr>


                    <tr>
                        <td>Name: <font color="red">*</font></td>
                        <td align="left"><?php echo $this->Form->text('Page.name', array('maxlength' => '254', 'size' => '20', 'label' => '', 'div' => false, 'class' => "form-inbox required")) ?>
                        </td>
                    </tr>


                        </table>

                 <table>       
                    <tr>
                        <td>&nbsp;</td>
                        <td> 
                            <?php echo $this->Form->submit('Submit', array('maxlength' => '50', 'size' => '30', 'label' => '', 'div' => false)) ?>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <?php echo $this->Form->end(); ?>
            </td>
        </tr>                      
    </table>

我用过 Configure::write('Routing.prefixes', 'admin'); 自动将 localhost://project_name/admin/pages/edit 等 url 解析为 pages 控制器的 admin_edit 操作。

编辑:当我 print_r($this->request->data); 在 if 块中,名称字段仍然包含旧值而不是我输入的新值。这是为什么?

魔猴忍者

这一行:-

if($this->Page->save($this->data)){

应该:-

if($this->Page->save($this->request->data)){

但是,您的方法的第一行是覆盖$this->request->data它应该包含您的表单数据!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章