• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

在Codeigniter中没有插入数据库的注册数据?重定向不工作?

php 来源:migs 5次浏览

我正在尝试创建一个简单的注册表单,但是无法将表单中的数据添加到我的数据库中?我错误地做了什么,数据不会发布到我的用户表中?在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> 


===========解决方案如下:

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

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

    } 

} 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)