php - Check duplicate data in CodeIgniter try to make a callback function -
i have registration form. here i'm able check duplicate email custom unique call function (don't try use is_unique). doesn't returns anything. here code.
controller -
public function add_member () { $this->load->library('form_validation'); $post_email = $this->input->post('email_id'); $this->form_validation->set_rules('email_id', 'email id/ username', 'required|trim|xss_clean|valid_email|callback_check_duplicate_email[' . $post_email . ']'); $this->form_validation->set_rules('password', 'your password', 'required|min_length[5]|max_length[12]|matches[confirm_password'); $this->form_validation->set_rules('confirm_password', 'password confirmation', 'required'); $this->form_validation->set_message('check_duplicate_email', 'this email exist. please write new email.'); if ($this->form_validation->run() == false) { // validation failed $this->load->view('member/reg_form'); } else { $data['email_id'] = $post_email; $data['password'] = md5($this->input->post('password')); $insert = $this->model_member->insert_data_to_db($data); if ($insert) { $success = "wao ! added our community."; $this->session->set_flashdata('message_success', $success); $this->load->view('member/success_page'); } else { $error = "hey email exists in our community."; $this->session->set_flashdata('message_error', $error); $this->load->view('member/reg_form'); } } } // callback function public function check_duplicate_email($post_email) { return $this->model_member->checkduplicateemail($post_email); }
model -
//for checking email existance public function checkduplicateemail($post_email) { $this->db->where('email_id', $email_id); $query = $this->db->get('my_registration_table'); $count_row = $query->num_rows(); if ($count_row > 0) { return true; } else { return false; } } // insert data db public function insert_data_to_db($data) { return $this->db->insert('my_registration_table', $data); }
when try submit form email exists. function doesn't stop me , doesn't show validation error message. here see what's problem? waiting help.
your problem in model function. @ following model function.
public function checkduplicateemail($post_email) { $this->db->where('email_id', $email_id); $query = $this->db->get('my_registration_table'); $count_row = $query->num_rows(); if ($count_row > 0) { //if count row return row; means have email address in database. must set false in sense. return false; // here change true false. } else { // doesn't return row means database doesn't have email return true; // , here false true } }
try this. hope works.
Comments
Post a Comment