cakePHP表单不向数据库发送数据

时间:2012-05-14 06:00:52

标签: php mysql database cakephp

我正在努力克服我的蛋糕代码(非常新的cakephp),并且得到了一些帮助。我的代码的目的是从表单中获取数据,当人员点击注册时,它会将数据发送到我的数据库但是没有发生,而是当我点击注册时页面正在重新加载。我目前正在使用cake 2.0和wamp server 2.2

这是我的个人控制器.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');

?>

编辑 - 尝试插入数据的表名称称为个人 任何帮助将不胜感激

2 个答案:

答案 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';}