Failed to update data in database via CakePHP form

I am writing an editing function to update news information, my code is:
this is the function of the controller file:

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 the 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 did not edit the name, image, news, I can’t save the news information, why does it work like this and how can I fix my function to edit the record?

+5
source share
2 answers

I think you are faced with a 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 a 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"));} 
+2
source

You should try debugging your code a bit:

Try the var_dump file move_uploaded_file to see what it returns.

Also var_dump $ this-> request-> data ['News'] and please post the results here, they may not be saved due to the data you are sending. If your model ID is not set, CakePHP will not know what to save / modify.

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

In another note, as a rule, it is better to place HTML in the views, and not in the controller (but this is more for your personal knowledge)

0
source

All Articles