简体   繁体   中英

Form not updating database table in cakephp code?

Hey everyone I am facing this problem the form is not updating when I click submit. It goes in if($this->request->is("post")){....} block but it is not updating.

Here is the code.

PagesController.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>

I have used Configure::write('Routing.prefixes', 'admin'); to automatically resolve url like localhost://project_name/admin/pages/edit to admin_edit action of pages controller.

Edit: When I print_r($this->request->data); in if block then the name field is still containing the old value not the new one I entered. Why is that?

This line:-

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

Should be:-

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

However, the first line of your method is overwriting $this->request->data which should contain your form data!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM