2013-08-28 14 views
2

我有一個註冊表單。在這裏,我可以通過我的自定義檢查重複的電子郵件是獨特的回調函數(不要嘗試使用is_unique)。但它不會返回任何東西。這是我的代碼。檢查CodeIgniter中的重複數據嘗試進行回調函數

控制器 -

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 is already exist. Please write a new email.'); 

    if ($this->form_validation->run() == FALSE) { 
     // validation failed then do that 

     $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 ! You are successfully added to our community."; 
      $this->session->set_flashdata('message_success', $success); 
      $this->load->view('member/success_page'); 
     } else { 
      $error = "Hey this email is already exists in our community."; 
      $this->session->set_flashdata('message_error', $error); 
      $this->load->view('member/reg_form'); 
     } 
    } 

} 

// My callback function 
public function check_duplicate_email($post_email) { 

    return $this->Model_member->checkDuplicateEmail($post_email); 

} 

型號 -

//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 to DB 
public function insert_data_to_db($data) { 
    return $this->db->insert('my_registration_table', $data); 
} 

當我嘗試提交表單與已經存在的電子郵件。此功能不會阻止我,並且不顯示驗證錯誤消息。有人會問這裏有什麼問題嗎?等待你的幫助。

回答

4

你的問題是在模型功能。看看下面的模型函數。

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 any row; that means you have already this email address in the database. so you must set false in this sense. 
     return FALSE; // here I change TRUE to false. 
    } else { 
     // doesn't return any row means database doesn't have this email 
     return TRUE; // And here false to TRUE 
    } 
} 

試試這個。希望工作。

+0

ohh。真是個錯誤。是的,現在工作。嘗試提交重複的電子郵件時,它會返回驗證錯誤。謝謝你的幫助。 – follett

0

如果我沒有弄錯,該函數應該在控制器中,而不是在模型中。

0
Call the right method in your model, as you defined in your controller.And your variable name should be same as argument passed in your function.I think it works. 
Model: 

public function insert_data_to_db($post_email) { 

    $this->db->where('email_id', $post_email); 

    $query = $this->db->get('my_registration_table'); 

    $count_row = $query->num_rows(); 

    if ($count_row > 0) { 
     return TRUE; 
    } else { 
     return FALSE; 
    } 
} 
0

您標記爲正確的答案是正確的,但您最終會遇到邏輯錯誤。

你傳遞您在$ post_email保存的電子郵件ID,但是笨讓您在正在修整,清洗變量傳遞等

那麼既然你節省副本發佈數據原樣,如果有空格或其他應該修剪的內容,但修剪後的版本匹配,則不匹配。

換句話說: 「[email protected]」可能不匹配「[email protected]」通過驗證,然後存儲「[email protected]」的2個副本。

$this->form_validation->set_rules('email_id', 'Email ID/ Username', 'required|trim|xss_clean|valid_email|callback_check_duplicate_email[email_id]'); 
相關問題