2017-12-03 146 views
0

我正在尝试创建一个简单的注册表单,但是无法将表单中的数据添加到我的数据库中?我错误地做了什么,数据不会发布到我的用户表中?在Codeigniter中没有插入数据库的注册数据?重定向不工作?

另外,如果验证失败,又是简单地加载视图再次做不当的事情?使用重定向只是失败,并说我应该尝试清除我的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

你叫,你加载模型下的模型,但不是函数尝试添加'$这个 - > signup_model->指数( );'也https://www.codeigniter.com/user_guide/general/models.html#loading-a-model – user4419336

+0

就是这样!谢谢! – migs

回答

2

您装入的模型,但不是功能

loading of a model guide

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

    } 

}