如何使用CodeIgniter验证数据是否成功?我搜索了codeigniter手册,我发现获取num_rows()和affected_rows()可以帮助我,但如何将其用于验证目的? Web中的示例使用SELECT语句而不是INSERT或UPDATE。这是我的一些代码。
public function create_user() {
$username = $this->input->post('username');
$password = md5($this->input->post('password'));
$lastname = ucwords($this->input->post('lastname'));
$firstname = ucwords($this->input->post('firstname'));
$email = $this->input->post('email');
$phone1 = $this->input->post('tel1');
$phone2 = $this->input->post('tel2');
$c_firstname = ucwords($this->input->post('c_firstname'));
$c_lastname = ucwords($this->input->post('c_lastname'));
$c_bldg = $this->input->post('c_bldg');
$c_address = $this->input->post('c_address');
$c_barangay = $this->input->post('c_barangay');
$c_city = $this->input->post('c_city');
$c_state = $this->input->post('c_state');
$c_country = $this->input->post('c_country');
$c_postal = $this->input->post('c_postal');
$c_tel = $this->input->post('c_tel');
$date = strtotime(date('Y-m-d H:i:s'));
$insert_user = array(
'user_login' => $username,
'timestamp' => $date,
'password' => $password,
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'phone' => $phone1
);
$insert_user_data = $this->db->insert('cscart_users',$insert_user);
//validate if data is inserted here. If inserted successfully proceed to next INSERT.
$get_id = $this->db->insert_id();
$insert_user_profile = array(
'user_id' => $get_id,
'b_firstname' => $c_firstname,
'b_lastname' => $c_lastname,
'b_address' => $c_address,
'b_state' => $c_state,
'b_city' => $c_city,
'b_country' => $c_country,
'b_zipcode' => $c_postal,
'b_phone' => $c_tel
);
$this->db->insert('cscart_user_profiles',$insert_user_profile);
}
答案 0 :(得分:2)
我假设你的tble在你的prmary键中设置了auto inc,codeigniter的函数insert_id如果我正确地回忆它会返回一个int值;如果插入不成功,则为0;如果插入不成功,则为粗增,因此可以安全地说您可以使用这些值。
//validate if data is inserted here. If inserted successfully proceed to next INSERT.
$get_id = $this->db->insert_id();
//check the value if it is 0 or not
if($get_id > 0){
$insert_user_profile = array(
'user_id' => $get_id,
'b_firstname' => $c_firstname,
'b_lastname' => $c_lastname,
'b_address' => $c_address,
'b_state' => $c_state,
'b_city' => $c_city,
'b_country' => $c_country,
'b_zipcode' => $c_postal,
'b_phone' => $c_tel
);
$this->db->insert('cscart_user_profiles',$insert_user_profile);
}