我不能使用CakePHP2.4通过表单将数据插入到数据库中

时间:2014-03-15 08:36:07

标签: php cakephp-2.0

我是CakePHP的新手,只是创造了我的东西。

我无法使用CakePHP2,4从表单中将数据添加到数据库中。我的控制器页面是StaffsController.php.View页面在视图中 - > Staff-> add.ctp,我的模型页面是Staff.php。表名 - staffs id int pk title var char(200)。

//My Code.


class StaffsController extends AppController 
{

    function index()
    {
    $this->set('posts', $this->Staff->find('all')); 
    }
    public function add() {
        if ($this->request->is('post')) {
            $this->Staff->create();
            $post_data = $this->request->data;
            if ($this->Staff->save($post_data)) {
                $this->Session->setFlash(__('New post saved successfully to the database'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to save the post to the database.'));
        }
}

}


// add.ctp

 <h1>Add Post</h1>
<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->end('Save');
?>

//My Model Page 

<? php
class Staff extends AppModel
 {

 }
?>

1 个答案:

答案 0 :(得分:0)

检查

<?php
echo $this->Form->create('Staff');
echo $this->Form->input('title');
echo $this->Form->end('Save');

dont use create('post');