我正在尝试创建一个简单的注册表单,但是无法将表单中的数据添加到我的数据库中?我做错了什么,数据不会发布到我的用户表?
如果验证失败,还只是再次加载视图不正确的事情?使用重定向只是失败,并说我应该尝试清除我的cookie。
我的代码如下。
注册控制器:
class Signup extends CI_Controller
{
public function index()
{
$this->load->view('templates/header');
$this->load->view('signup');
$this->load->view('templates/footer');
}
function signup_validation() {
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|min_length[6]|matches[password]');
if ($this->form_validation->run()) {
$this->load->model('signup_model');
redirect('login');
}
else {
$this->load->view('templates/header');
$this->load->view('signup');
$this->load->view('templates/footer');
}
}
}
注册模式:
class Signup_model extends CI_Model {
public function index()
{
$url = url_title($this->input->post('user'), 'dash', true);
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
return $this->db->insert('users', $data);
}
}
注册视图
<form action="<?php echo site_url('signup/signup_validation') ?>" method="POST">
<h5>First Name</h5>
<input type="text" name="first_name" value="" />
<span><?php echo form_error('first_name'); ?></span>
<h5>Last Name</h5>
<input type="text" name="last_name" value="" />
<span><?php echo form_error('last_name'); ?></span>
<h5>Username</h5>
<input type="text" name="username" value="" />
<span><?php echo form_error('username'); ?></span>
<h5>Email Address</h5>
<input type="text" name="email" value="" />
<span><?php echo form_error('email'); ?></span>
<h5>Password</h5>
<input type="text" name="password" value="" />
<span><?php echo form_error('password'); ?></span>
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" />
<span><?php echo form_error('passconf'); ?></span>
<div><input type="submit" value="Register" /></div>
</form>
答案 0 :(得分:2)
您已加载模型但未加载函数
function signup_validation() {
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[4]|is_unique[users.username]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required|min_length[6]|matches[password]');
if ($this->form_validation->run()) {
$this->load->model('signup_model');
$this->signup_model->index();
redirect('login');
} else {
$this->load->view('templates/header');
$this->load->view('signup');
$this->load->view('templates/footer');
}
}
}