表格在cakephp中提交

时间:2016-04-02 11:04:38

标签: php cakephp

我是cakephp中的新手,我想在数据库中插入表单数据,但不插入值。哪里错了? 这是我的观点(首先我想只添加一个字段)。提前致谢

在视图中 -

echo $this->Form->create('Post');

echo $this->Form->input('title');

echo $this->Form->end('Add');

在controller =

public function index()

{

$this->layout=false;

if($this->is->request('post'):

$this->request->data["Post"]["title"]=$this->request->data["Post"]["title"]

$this->Post->save($this->request->data);

endif;

}

2 个答案:

答案 0 :(得分:0)

您需要检查控制器代码。

试试这个:

 public function index() {

     $this->layout=false;

     if($this->request->is('post')) {

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

            echo "Save successful!";

        } else {

            echo "Save failed!";
        }

        exit;

     }
 }

希望这有帮助。

答案 1 :(得分:0)

在这里,我可以举例说明使用多个字段插入cakephp中的任何记录

只需看看功能,你可能有个好主意

        $data = $this->request->data;
        if (!empty($data)) {
            $ret_save = $this->Post->save();
            if ($ret_save) {
                $this->Session->setFlash(__('Save successful!'));
                $this->redirect(array('controller' => 'controller_name', 'action' => 'index'));
            }else{
               echo "Save failed!";
           }

希望此代码能为您提供帮助。

相关问题