doutuoben6908
2015-02-20 15:10
浏览 30
已采纳

CakePhp:无法向数据库添加行

i'm try to add a new row to a table of my database by using CakePhp framework, but something it's not work properly. This is my add.ctp

echo $this->Form->create(array('type' => 'get'));
echo $this->Form->input('username');
echo $this->Form->input('password');

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

This is controller add function:

public function add() {
    if ($this->request->is('get')) {
        $this->Admin->create();
        if ($this->Admin->save($this->request->data)) {
            debug("done");
            $this->Session->setFlash(__('Your post has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
    }
}

and this is my Admin Model class:

class Admin extends AppModel {
    public $validate = array(
        'username' => array(
        'rule' => 'notEmpty'
    ),
    'password' => array(
        'rule' => 'notEmpty'
    )
);

but new row is never added to my Admins table. Database works fine (i can retrieve all rows inside table). I'm working on localhost with xampp

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutan1905 2015-02-20 15:44
    已采纳

    Its not working because your $this->request->data is not populated with information you sent with your request. Simply CakePHP handles post and get differently.

    All POST data can be accessed using CakeRequest::$data.

    If for some unknown reason you want to send your password with form using 'get' you can access it like this

    $data['username'] = $this->params['url']['username'];
    $data['password'] = $this->params['url']['password'];
    

    Also please take a look at this 'when do you use post, when do you use get'

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题