这是我的个人控制器.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');
?>
编辑 - 尝试插入数据的表名称称为个人 任何帮助将不胜感激
答案 0 :(得分:1)
我认为代码中的错误在下面给出的行中。
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
答案 1 :(得分:0)
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.');
}
}
}
将此代码更改为
function addIndividual(){
if($this->request->is('post')){
{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.');
}
}
}
另请更改您的视图文件,如下所示。
<h2>Please enter your details</h2>
<?php
echo $this->Form->create('Individual', array('action'=>'addIndividual'));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Title: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'First Name: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Surname: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Street Address: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Suburb: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'State: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Country: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Email: '));
echo $this->Form->input('FieldnameasinDB',array('label'=>'Password: '));
echo $this->Form->end('Click here to Register');
?>
像这样更改模型
<?php class Individual extends AppModel{ var $name='Individual';
public $useTable = 'tableName';
public $primaryKey = 'userid';}