我正在努力解决我的蛋糕代码(对 cakephp 来说非常新)并且需要一些帮助。我的代码的目的是从表单中获取数据,当用户点击注册时,它会将数据发送到我的数据库,但这并没有发生,而是当我点击注册时,页面正在重新加载。我目前使用 cake 2.0 和 wamp server 2.2
这是我的 individualcontroller.php 中的添加函数的代码
function addIndividual(){
if($this->request->is('individuals')){
{if ($this->Individual->save($this->request->data)) {
$this->Session->setFlash('The user has been saved');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The user could not be saved. Please, try again.');
}
}
}
} 代码在这里
这是我的 add_individual.ctp 中的代码,包括表单
<h2>Please enter your details</h2>
<?php
echo $this->Form->create('individuals', array('action'=>'addIndividual'));
echo $this->Form->input('Title: ');
echo $this->Form->input('First Name: ');
echo $this->Form->input('Surname: ');
echo $this->Form->input('Street Address: ');
echo $this->Form->input('Suburb: ');
echo $this->Form->input('State: ');
echo $this->Form->input('Country: ');
echo $this->Form->input('Email: ');
echo $this->Form->input('Password: ');
echo $this->Form->end('Click here to Register');
?>
编辑 - 我尝试插入数据的表的名称称为 individual 任何帮助将不胜感激
最佳答案
我认为您的代码中的错误位于下面给出的行中。
if($this->request->is('individuals'))
此处$this->request->is
检查请求类型,并且您已在此处分配individuals
,但这不是请求类型。请求类型包括 post
、'put'、'ajax'、'GET' 等。因此,请检查您表单的方法并将其放入 $this->request->is()
中。请查看这个网址:
http://book.cakephp.org/2.0/en/controllers/request-response.html
关于php - cakePHP 表单不向数据库发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10578117/