2012-01-24 52 views
0

出於某種原因,當我提出我的形式得到什麼提交回是「有創建用戶的一個問題,請再試一次!」但它仍然會創建一個新的數據庫條目,我不知道爲什麼。我在我的表單上運行測試,但我提交的數據應該給我一個成功的信息。有任何想法嗎?沒有返回所需的錯誤/成功的消息

編輯:我試過2天現在似乎仍不能算出這個。

控制器:

function register_submit() 
{ 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric|strtolower'); 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[6]|max_length[12]|alpha_numeric'); 
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|xss_clean|matches[password]|min_length[6]|max_length[12]|alpha_numeric'); 
    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|alpha'); 
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|alpha');  

    if (!$this->form_validation->run()) 
    { 
     echo json_encode(array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!'));  
    } 
    else 
    {       
     $user_data = $this->kow_auth->create_user($this->input->post('username'), $this->input->post('email'), $this->input->post('password'), $this->input->post('first_name'), $this->input->post('last_name')); 
     if ($user_data == FALSE)  
     { 
      echo json_encode(array('error' => 'yes', 'message' => 'There was a problem creating the user! Please try again!')); 
     }  
     else 
     { 
      $data['activation_period'] = 48; 
      $this->kow_auth->send_email('activate', $data['email'], $data); 
      unset($data['password']); 
      echo json_encode(array('sucess' => 'yes', 'message' => 'You have successfully registered. Check your email address to activate your account!')); 
     } 
    } 
} 

庫:

/** 
* Create new user on the site and return some data about it: 
* user_id, username, password, email, new_email_key (if any). 
* 
* @param string 
* @param string 
* @param string 
* @param string 
* @param string 
* @return array 
*/ 
function create_user($username, $email, $password, $first_name, $last_name) 
{ 
    if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username)) 
    { 
     return FALSE; 
    } 
    elseif (!$this->ci->users->is_email_available($email)) 
    { 
     return FALSE; 
    } 
    else 
    { 
     $genPassHash = $this->ci->genfunc->GenPassHash($password); 

     $max_user_id = $this->ci->users->get_max_users_id(); 

     $data = array(
      'username'  => $username, 
      'password'  => $genPassHash[0], 
      'password2'  => $genPassHash[1], 
      'email'   => $email, 
      'first_name' => $first_name, 
      'last_name'  => $first_name, 
      'new_email_key' => md5(rand().microtime()), 
      'user_id'  => $max_user_id, 
      'ip_address' => $this->ci->input->ip_address() 
     ); 

     if (($result = $this->ci->users->create_user($data)) == FALSE) 
     { 
      $data['user_id'] = $res['user_id']; 
      $data['password'] = $password; 
      unset($data['last_ip']); 
      return $data; 
     } 
    } 
    return FALSE; 
} 

用戶型號:

/** 
* Create new user record 
* 
* @param array 
* @return bool 
*/ 
function create_user($data) 
{ 
    $data['date_created'] = date('Y-m-d H:i:s'); 

    $this->db->set('username', $data['username']); 
    $this->db->set('password', $data['password']); 
    $this->db->set('password2', $data['password2']); 
    $this->db->set('email', $data['email']); 
    $this->db->set('first_name', $data['first_name']); 
    $this->db->set('last_name', $data['last_name']); 
    $this->db->set('ip_address', $data['ip_address']); 
    $this->db->set('date_created', $data['date_created']); 

    $query = $this->db->insert('users'); 

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

那麼無論是在USER_DATA它沒有= = FALSE。所以可能是回聲user_data看看裏面有什麼會幫助你。 –

+0

沒錯,如果數據輸入正確且完全如預期,但是您報告錯誤,最好的辦法就是檢查您的創建並評估返回值 – horatio

+0

我在它後面做了一個echo和print_r,但沒有任何內容。所以這沒有幫助。 –

回答

0

我想你可能有在某些時候在所有這些插入一些SQL錯誤和$查詢是假,你應該閱讀有關交易,以避免該部分插入......它是關於邏輯居多。 Basicaly你不想perfprm任何插件如果任何在你的邏輯插件會失敗,因此去閱讀有關的交易。你沒有指定什麼數據庫平臺的使用,所以我不能給出一個肯定的鏈接...如果你用mysql這裏是一個鏈接:http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html