简体   繁体   中英

unable to update data in database through CakePHP form

I write a edit function to update news's info, my code:
this is controller file function:

public function editnews($id) { 
    $this->layout = "news"; //tynemc call by this statement
    $this->loadModel('News'); //model call 
    $this->loadModel('Category'); //load news category
    if ($this->request->is('post')) {
         if (move_uploaded_file($this->request->data['News']['image_url']['tmp_name'], WWW_ROOT . 'media/'. $this->request->data['News']['image_url']['name'])) {

            $this->request->data['News']['image_url'] = time() . $this->request->data['News']['image_url']['name'];
        }$this->News->save($this->request->data['News']);//data save by this statement
        $msg = '<div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <strong> News update successfully </strong>
        </div>';
        $this->Session-> setFlash($msg);
        return $this->redirect('editnews');
    }
    if (!$this->request->data) { // id wise data search
        $data = $this->News->findById($id);
        $this->request->data = $data;

    }
    $this->set('categories', $this->Category->find("list"));//categories load in dropdown
}

this is ctp file form action code:

<?php echo $this->Form->create('News', array(
                        'inputDefaults' => array(
                            'label' => false,
                            'div' => false
                        ),
                        'id' => 'form-validate',
                        'class' => 'form-horizontal',
                        'novalidate' => 'novalidate',
                        'enctype' => 'multipart/form-data',
                        'controller' => 'admins',
                        'action' => 'editnews'
                            )
                    );
                    ?>

i didn't edit title,image,newsdetail, I can't save news info, why it work like this and how can i correct my function to edit a record ?

I think you facing form action problem. try this code for action.

  <?php

                    echo $this->Form->create("News",array(
                        'inputDefaults' => array(
                            'label' => false,
                            'div' => false
                        ),
                        'url' => array(
                            'controller' => 'admins',
                            'action' =>'editnews'
                            ), 
                        'id' => 'form-validate',
                        'class' => 'form-horizontal',
                        'novalidate' => 'novalidate',
                        'enctype' => 'multipart/form-data'
                        )
                    );
                ?>

and try this code as function.

    public function editnews($newsid = null) {
        $this->layout = "news";
        $this->loadModel('News');
        $this->loadModel('Category');
        $this->News->id = $newsid; 
        if ($this->request->is('get')) { 
            $this->request->data = $this->News->read();
        } 
        else {
            $data = $this->request->data;
            }
            if ($this->News->save($data)) { 
                $this->Session->setFlash("Newsletter angelegt");

               $msg = '<div class="alert alert-success">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
               <strong> News update successfully </strong>
              </div>';
                $this->Session->setFlash($msg);
                $this->redirect(array( 'controller' => "admins", "action" => "manage_newses"));
            } 
            else 
            {
                $this->Session->setFlash("not updated");
                $this->render();
           }
        }
        if (!$this->request->data) {
           $data = $this->News->findById($id);
           $this->request->data = $data;

        }
        $this->set('categories', $this->Category->find("list"));}

You should try debugging your code a bit :

Try to var_dump the move_uploaded_file to see what it returns.

Also var_dump $this->request->data['News'] and please put the results here, it might not save because of the data you send. If the ID of your model is not set, CakePHP won't know what to save / edit.

If you can't var_dump, you can use $this->log($myVar, 'debug'), and then check your file in app/tmp/logs/debug.log

On another note, it is usually better to put HTML in views, not in controller (but it's more for your personal knowledge)

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