how to check if username already exists in codeigniter

后端 未结 8 1325
无人共我
无人共我 2020-12-20 00:33

I am working on a site in codeigniter.I am not so expert using framework.Here I have to check if email already exists in database.I have coded the required functionality but

相关标签:
8条回答
  • 2020-12-20 00:52

    You have an error in the mysql query:-

    SELECT * WHERE ` = 'muraddnw@gmail.com' LIMIT 1
    

    Your code has not specified which table to query from. So use following:-

    $this->db->select('id');
    $this->db->from('tablename');
    $this->db->where('email', $email);
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-20 00:56
    $this->form_validation->set_rules('username', 'userName', 'required|callback_exists_username');
    
    
    #uniqueness of username
        function exists_username($str)
        {
            $record_id = $this->input->post('record_id');
            $condition = array('user_id !='=>$record_id,'username'=>$str);
            $value =GetAllRecord('user_master',$condition,$is_single=true);
            if (count($value) == 0)
            {
                return TRUE;
            }
            else
            {
                $this->form_validation->set_message('exists_username', 'username already exists!');
                return FALSE;
            }
        }
    
    0 讨论(0)
  • 2020-12-20 00:57

    Use is_unique[table.field] add table name and field.

    $this->form_validation->set_rules(
        'email', 'Email', 'trim|required|valid_email|is_unique[table.field]|callback_isEmailExist'
    );
    
    0 讨论(0)
  • 2020-12-20 00:58

    you can check if a field is unique or not by using form_validator function is_unique[table_name.field_name]

    0 讨论(0)
  • 2020-12-20 01:02

    no need to a make a special function, this is enough

    $this->form_validation->set_rules(
        'email', 'Email', 'trim|required|valid_email|is_unique'
    );
    
    0 讨论(0)
  • 2020-12-20 01:10

    Your problem is in the set_rules line. Here you use both is_unique and a callback function. You have to use anyone of that. If you use a call_back function to check duplicate data; doesn't need to use is_unique. For this wrong you get that error Just remove the is_unique from there.

    $this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_isEmailExist'); // removed is_unique
    

    Try this out and let me know.

    0 讨论(0)
提交回复
热议问题