CakePhp:无法将行添加到数据库
CakePhp: unable to add row to database
我尝试使用 CakePhp 框架向我的数据库的 table 添加一个新行,但它无法正常工作。
这是我的 add.ctp
echo $this->Form->create(array('type' => 'get'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Save');
这是控制器添加功能:
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'));
}
}
}
这是我的管理模型 class:
class Admin extends AppModel {
public $validate = array(
'username' => array(
'rule' => 'notEmpty'
),
'password' => array(
'rule' => 'notEmpty'
)
);
但新行从未添加到我的管理员 table。数据库工作正常(我可以检索 table 内的所有行)。
我正在使用 xampp
在本地主机上工作
它不起作用,因为你的 $this->request->data is not populated with information you sent with your request. Simply CakePHP handles post and get 不同。
All POST data can be accessed using CakeRequest::$data.
如果出于某种未知原因您想使用 'get' 发送密码和表单,您可以像这样访问它
$data['username'] = $this->params['url']['username'];
$data['password'] = $this->params['url']['password'];
另请看一下this 'when do you use post, when do you use get'
我尝试使用 CakePhp 框架向我的数据库的 table 添加一个新行,但它无法正常工作。 这是我的 add.ctp
echo $this->Form->create(array('type' => 'get'));
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Save');
这是控制器添加功能:
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'));
}
}
}
这是我的管理模型 class:
class Admin extends AppModel {
public $validate = array(
'username' => array(
'rule' => 'notEmpty'
),
'password' => array(
'rule' => 'notEmpty'
)
);
但新行从未添加到我的管理员 table。数据库工作正常(我可以检索 table 内的所有行)。 我正在使用 xampp
在本地主机上工作它不起作用,因为你的 $this->request->data is not populated with information you sent with your request. Simply CakePHP handles post and get 不同。
All POST data can be accessed using CakeRequest::$data.
如果出于某种未知原因您想使用 'get' 发送密码和表单,您可以像这样访问它
$data['username'] = $this->params['url']['username'];
$data['password'] = $this->params['url']['password'];
另请看一下this 'when do you use post, when do you use get'