Codeigniter - Update email only if the email doesn't exist in the database

前端 未结 3 829
借酒劲吻你
借酒劲吻你 2021-01-17 04:00

I have an update page for my users where they can edit their name, email and other info.

So far, they can edit everything. Including their email. They can enter an e

3条回答
  •  清歌不尽
    2021-01-17 04:44

    You can try the following code :

    $this->form_validation->set_rules('email', 'lang:email', 'trim|required|valid_email|callback__is_unique_email[email]');
    

    and the callback function should look like this :

    public  function _is_unique_email($value, $field){
        $result = $this->db->where('uid !=', $this->session->userdata('uid'))
                        ->where($field, $value)
                        ->get('users')
                        ->row_array();
    
        if ($result) {
            $this->form_validation->set_message('_is_unique_email', $this->lang->line('_is_unique_'));
            return false;
        }
    
        return true;
    }
    

提交回复
热议问题