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'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件