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 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数