我正在使用 codeigniter 并一次插入一条记录。但问题是 $this->db->insert_id();每次都返回 0,但是记录已成功创建。我无法弄清楚。这是常见情况还是我犯了一些愚蠢的错误。我使用 email_id 作为主键,使用手机号作为唯一键。
这是我的模态代码:
function signup_insert($data) {
$result = $this->db->insert('registration_detail', $data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
这是我的控制器代码:
function insert_signup() {
$email = $this->input->get('email');
$name = $this->input->get('name');
$password1 = $this->input->get('password1');
$password2 = $this->input->get('password2');
$phone = $this->input->get('phone');
$country = $this->input->get('country');
$data = array(
'email' => $email,
'name' => $name,
'pwd' => $password1,
'phone' => $phone,
'country' => $country
);
$insert_id = $this->signup->signup_insert($data);
print_r($insert_id);
}
Hence $this->db->insert_id()
执行数据库插入时的插入 ID 号。
并且你的数据库中没有自动递增的id
检查数据是否插入使用
$this->db->affected_rows()
执行“写入”类型查询时显示受影响的行数
(插入、更新等)。
Models
function signup_insert($data) {
$result = $this->db->insert('registration_detail', $data);
$rows = $this->db->affected_rows();
if($rows>0){
return $rows;
}else{
return FALSE;
}
}
Read http://www.codeigniter.com/user_guide/database/helpers.html http://www.codeigniter.com/user_guide/database/helpers.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)