CodeIgniter:我在提交注册表时遇到此数据库错误1048。

时间:2016-10-25 20:17:19

标签: php database codeigniter phpmyadmin mysql-insert-id

我在提交注册表时向localhost发出此数据库错误,该注册表是在将用户值插入数据库时​​。

发生数据库错误 错误号码:1048 专栏' first_name'不能为空 INSERT INTO用户(first_name,last_name,用户名,电子邮件,密码)VALUES(NULL,NULL,NULL,'',' d41d8cd98f00b204e9800998ecf8427e')

这是我的代码(application / model / model_user)

<?php

class Model_users extends CI_Model{

    public function can_log_in(){

        $this->db->where('email', $this->input->post('email'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('users');

        if($query->num_rows() == 1){
            return TRUE;
        }
        else {
            return FALSE;
        }  
    }

    public function add_user(){

        $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 = md5($this->input->post('password'));

        $sql = "INSERT INTO users(first_name, last_name, username, email, password) 
        VALUES( " .$this->db->escape($first_name). ",
                " .$this->db->escape($last_name). ",
                " .$this->db->escape($username). ",
                '".$email."',
                '".$password."')";

        $query = $this->db->query($sql);
        if($query){
            return TRUE;
        }
        else{
            return FALSE;
        }
    }
}

?>

我也尝试了这个插入查询但是同样的db错误

 $new_members = 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' => md5($this->input->post('password'))
        );

        $insert = $this->db->insert('users', $new_members);
        if($insert){
            return TRUE;
        } else {
            return FALSE;

以下是控制器文件(application / controllers / main)。此文件中没有此类错误,表单验证也可正常工作。

<?php

class Main extends CI_Controller{

    public function index() {
        $this->login();
    }

    public function login(){
        $this->load->view('login');   
    }

    public function signup(){
        $this->load->view('signup');   
    }



    public function members(){
    if($this->session->userdata('is_logged_in')){
        $this->load->view('members');
    }
    else {
        redirect('main/restricted');
    }  
    }

    public function restricted(){
        $this->load->view('restricted');
    }

    public function login_validation(){
        $this->load->helper('security');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('email','Email','required|trim|xss_clean|callback_validate_credentials');
        $this->form_validation->set_rules('password','Password','required|md5|trim');

        if($this->form_validation->run()){

            $data = array(
                'email' => $this->input->post('email'),
                'is_logged_in' => 1
            );
            $this->session->set_userdata($data);

            redirect('main/members');
        }
        else{
            $this->load->view('login');
        }  
    }

    public function signup_validation(){
        $this->load->helper('security');
        $this->load->library('form_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('email','Email','required|trim|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('username','Username','required|trim|is_unique[users.username]');
        $this->form_validation->set_rules('password','Password','required|trim');
        $this->form_validation->set_rules('cpassword','Confirm Password','required|trim|matches[password]');

        $this->form_validation->set_message('is_unique','That email address already exist!');

        if($this->form_validation->run()){

            //echo 'Validation Successful.';
            redirect('main/add_user');
        }
        else {
           //Validation failed
            $this->load->view('signup');
        }
    }

    public function add_user(){
        $this->load->model('model_users');
        $result = $this->model_users->add_user();

        if($result){
           $this->load->view('success'); 
        }
        else{ //this should never happen
            echo 'Sorry, there is a problem with a site. Please try again!' ;
        }  
    }

    public function validate_credentials(){
        $this->load->model('model_users');

        if($this->model_users->can_log_in()){
            return TRUE;
        }
        else{
            $this->form_validation->set_message('validate_credentials','Sorry incorrect Email ID or Password');
            return FALSE ;
        }

    }

    public function logout(){
        $this->session->sess_destroy();
        redirect('main/login');
    }

}
?>

1 个答案:

答案 0 :(得分:1)

试试这个

在控制器中

public function signup_validation(){
    $this->load->helper('security');
    $this->load->library('form_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('email','Email','required|trim|valid_email|is_unique[users.email]');
    $this->form_validation->set_rules('username','Username','required|trim|is_unique[users.username]');
    $this->form_validation->set_rules('password','Password','required|trim');
    $this->form_validation->set_rules('cpassword','Confirm Password','required|trim|matches[password]');

    $this->form_validation->set_message('is_unique','That email address already exist!');

    if($this->form_validation->run())
    {
        $this->load->model('model_users');
        $result = $this->model_users->add_user();

        if($result == TRUE){
           $this->load->view('success'); 
        }
        else
        { 
            echo 'Sorry, there is a problem with a site. Please try again!' ;
        } 
    }
    else {
       //Validation failed
        $this->load->view('signup');
    }
}

在模型中

public function add_user(){

    $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 = md5($this->input->post('password'));

    $data = array(
            'first_name' => $this->db->escape($first_name),
            'last_name' => $this->db->escape($last_name),
            'username' => $this->db->escape($username),
            'email' => $this->db->escape($email),
            'password' => $this->db->escape($password)
    );

    if(!$this->db->insert('users', $data))
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}