我正在开发cakephp mvc架构中的一个注册表单,但是当我提交表单数据时没有存储在数据库中。
数据库:
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');
}
}
}
}
?>
请解决问题。
答案 0 :(得分:0)
尝试
$这 - &GT;用户 - &GT;保存($这 - &GT; PARAMS [ '数据']);
或
$这 - &GT;用户 - &GT;保存($这 - &GT;请求 - &GT;数据);
答案 1 :(得分:0)
蛋糕溶液2.X
查看档案
<?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!'));
}
}
}