0

我正在为我的蛋糕代码而苦苦挣扎(对 cakephp 来说非常新)并且正在寻求一些帮助。我的代码的目的是从表单中获取数据,当人们点击注册时,它会将数据发送到我的数据库,但这没有发生,而是当我点击注册时页面正在重新加载。我目前正在使用 cake 2.0 和 wamp server 2.2

这是我的 individualcontroller.php 中的 add 函数的代码

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');

?>

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

4

2 回答 2

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

于 2012-05-14T06:24:36.417 回答
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';} 
于 2012-05-14T06:06:25.350 回答