无法通过CakePHP表单更新数据库中的数据

时间:2015-07-01 08:51:20

标签: php mysql cakephp

我写了一个编辑功能来更新新闻的信息,我的代码:
这是控制器文件功能:

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
}

这是ctp文件表格动作代码:

<?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'
                            )
                    );
                    ?>

我没有编辑标题,图片,newsdetail,我无法保存新闻信息,为什么它会像这样工作,如何更正我的功能来编辑记录?

2 个答案:

答案 0 :(得分:1)

我认为你面临形式行动问题。尝试使用此代码进行操作。

  <?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'
                        )
                    );
                ?>

并尝试将此代码作为函数。

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

答案 1 :(得分:0)

您应该尝试稍微调试一下代码:

尝试var_dump move_uploaded_file以查看它返回的内容。

另外var_dump $ this-&gt; request-&gt; data ['News']请将结果放在此处,因为您发送的数据可能无法保存。如果未设置模型的ID,CakePHP将不知道要保存/编辑的内容。

如果你不能使用var_dump,你可以使用$ this-&gt; log($ myVar,'debug'),然后在app / tmp / logs / debug.log中检查你的文件

另一方面,通常最好将HTML放在视图中,而不是放在控制器中(但更多是为了您的个人知识)