在我的数据库中,我的申请表格数据未存储

时间:2014-04-16 13:36:12

标签: php cakephp

我开发了一个form.it将无法正常工作。我的代码全部是数据库,模型,控制器类

这是数据库代码。

数据库:

CREATE TABLE users (
    id INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    username VARCHAR( 40 ) NOT NULL ,
    password VARCHAR( 40 ) NOT NULL ,
    email VARCHAR( 255 ) NOT NULL ,
    first_name VARCHAR( 40 ) NOT NULL ,
    last_name VARCHAR( 40 ) NOT NULL
) 

模型类:

<?php
    class User extends AppModel{
        var $name='User';
    }
?>

查看课程:

<html>
    <form action="../users/register" method="post">
        <p>Please fill out the form below to register an account.</p>
        <label>Username:</label><input name="username" size="40" />
        <label>Password:</label><input type="password" name="password" size="40"/>
        <label>Email Address:</label><input name="email" size="40" maxlength="255" />
        <label>First Name:</label><input name="first_name" size="40" />
        <label>Last Name:</label><input name="last_name" size="40" />
        <input type="submit" value="register" />
    </form>
</html>

控制器类:

<?php
class UsersController extends AppController{

    function register(){
        if (!empty($this->params['form'])){
            if($this->User->save($this->params['form'])){
                $this->flash('Registration Successful','/users/register');
            } else{
                $this->flash('Not succeeded','/users/register');
            }
        }
    }

}
?>

请解决我的问题

1 个答案:

答案 0 :(得分:1)

请完整地执行以下代码。

此处更改路径$ this-&gt; redirect('/');如果您不想重定向,请删除该行

查看:

<?php 
echo $this->Form->create('User', array(
        'controller' => 'users', 'action'=>'register'
    ));

    echo $this->Form->input('username', array(
        'size'=>40,
        'type' => 'text',
        'placeholder'=>__('Your username'),
    ));
    echo $this->Form->input('password', array(
        'size'=>40,
        'type' => 'password',
        'placeholder'=>__('password'),
    ));
    echo $this->Form->input('email', array(
        'size'=>40,
        'type' => 'email',
        'placeholder'=>__('your@email.com'),
    ));

    echo $this->Form->input('first_name', array(
        'size'=>40,
        'type' => 'text',
        'placeholder'=>__('First name'),
    ));

    echo $this->Form->input('last_name', array(
        'size'=>40,
        'type' => 'text',
        'placeholder'=>__('Last name'),
    ));
    echo $this->Form->end('Submit');
?>

<强>控制器:

public function register(){
    if($this->request->is('post')){
        if($this->User->save($this->request->data)) {

            $this->Session->setFlash(__('Registered'));
            $this->redirect('/');
        } else {
            $this->Session->setFlash(__('Something went wrong!'));
        }
    }
}