2015-11-04 63 views
1

我的回調函數有問題。我創建了一個回調,可以驗證輸入的用戶名是否與現有的用戶名相同。如果不相同,則驗證是否已經存在。如果不存在,則驗證必須通過。總是在Codeigniter form_validation回調中返回TRUE

這是我在這個過程中所做的:

  1. 我嘗試正常驗證領域(需要MAX_LENGTH,MIN_LENGTH)==已經存在的用戶名OK
  2. 輸入= OK
  3. 輸入一個新的username = NOT OK

當我嘗試輸入新的用戶名。它說它已經存在。當我檢查數據庫時,它已經更新了我的用戶名。

這是我在我的代碼:

控制器

$this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[30]|username_check'); 
$this->form_validation->set_rules('lastname', 'Last Name', 'required'); 
$this->form_validation->set_rules('firstname', 'First Name', 'required'); 
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email'); 
$this->form_validation->set_rules('phone1', 'Primary Contact', 'required'); 
$this->form_validation->set_rules('address1', 'Primary Address', 'required'); 
$this->form_validation->set_rules('birthday', 'Birthday', 'required'); 
$this->form_validation->set_rules('gender', 'Gender', 'required'); 

if($this->input->post('password1')) { 
    $this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[8]|max_length[16]'); 
    $this->form_validation->set_rules('password2', 'Confirm Password', 'required|trim|matches[password1]'); 
} 

if($data['role_id'] == 1 || $data['role_id'] == 2) { 
    $this->form_validation->set_rules('role', 'User Role', 'required'); 
    $this->form_validation->set_rules('status', 'Status', 'required'); 
} 

if($this->form_validation->run() == TRUE) { 

    $username = strtolower($this->input->post('username')); 
    $password = $this->input->post('password1'); 

    /** some code here **/ 

幫助我的回調

if(!function_exists('is_username_exists')) { 
     function is_username_exists($username) { 

      $ci =& get_instance(); 

      $ci->db->select('user_id'); 
      $ci->db->where('username', $username); 
      $checkValid = $ci->db->get('user'); 
      $num = $checkValid->num_rows(); 

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


     } 
    } 


    if(!function_exists('username_check')) { 
     function username_check($username) { 

      $ci =& get_instance(); 

      $current_value = $ci->flx_user->getUsername(); 

      //check if the input value is same as the saved value 
      if($current_value != $username) { //if not same check if already existed 

       $is_available = is_username_exists($username); 

       if(!$is_available) { 
        $ci->form_validation->set_message('username_check', 'Username already taken. Please try again.'); 
        return FALSE; 
       } else { 
        return TRUE; 
       } 

      } else { 
       // if the same just bypass it 
       return TRUE; 
      } 
     } 
    } 

你能幫助我嗎?

+0

豈不是:'其中username =「」。 $ ci-> db-> escape($ username)。「''? (與周圍的單引號?) –

回答

2

如果用戶存在於數據庫中,你必須return FALSEreturn TRUE

,改變你的查詢

$ci->db->select("user_id"); 
$ci->db->where('username',$username); 
$checkValid=$ci->db->get("user"); 
$num = $checkValid->num_rows(); //counting row 

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

謝謝。我會嘗試.. :) – Jerielle

+0

好吧,我已經嘗試過,但仍然同樣的效果。 – Jerielle

+0

我已經用修改後的代碼更新了上面的代碼。 – Jerielle

1

使用num_rows()更好的體驗。另請參閱return報表

$queryValid = $ci->db->query("SELECT * FROM user WHERE username = " . $ci->db->escape($username) . ""); 
    //$checkValid = $queryValid->row_array(); <-- remove this line 

    if($queryValid->num_rows() > 0) { 
     return FALSE; 
    } else { 
     return TRUE; 
    }