How to check if email already exists in db with CodeIgniter
Suppose, you have user
table and email
column. So you have to add this line in form validation
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[users.email]');
NoteAnd need an unique index in your email
column
check Documentation
Add a rule:
$this->form_validation->set_rules('email', 'Email', 'callback_rolekey_exists');
In the same Controller:
function rolekey_exists($key) { $this->roles_model->mail_exists($key);}
In Model,
function mail_exists($key){ $this->db->where('email',$key); $query = $this->db->get('users'); if ($query->num_rows() > 0){ return true; } else{ return false; }}
Best Way
$this->form_validation->set_rules( 'email', 'Email', 'valid_email|required|is_unique[usr_user.user_email]', array('is_unique' => 'This %s already exists.') );