gpt4 book ai didi

php - 如何使用CodeIgniter判断表中插入数据成功还是失败?

转载 作者:行者123 更新时间:2023-11-29 08:07:39 25 4
gpt4 key购买 nike

如何使用 CodeIgniter 验证数据是否成功?我搜索了 codeigniter 手册,发现获取 num_rows() 和affected_rows() 可以帮助我,但如何将其用于验证目的?网络中的示例使用 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);

}

最佳答案

我假设你的表在你的主键中设置了 auto inc,如果我没记错的话,codeigniter 的函数 insert_id 返回一个 int 值;如果插入不成功,则返回 0;如果插入成功,则返回粗略的递增 id,因此可以肯定地说,您可以使用这些值来代替。

 //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);
}

关于php - 如何使用CodeIgniter判断表中插入数据成功还是失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com