$这 - > DB-> INSERT_ID(); 每次在codeigniter中返回0

Imr*_*ran 3 php mysql codeigniter

我正在使用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;
    }
Run Code Online (Sandbox Code Playgroud)

这是我的控制器代码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Sat*_*aty 11

于是 $this->db->insert_id()

执行数据库插入时的插入ID号.

并且数据库中没有自动增加的id

要检查数据是插入还是不使用

$this->db->affected_rows()
Run Code Online (Sandbox Code Playgroud)

执行"写入"类型查询(插入,更新等)时,显示受影响的行数.

楷模

function signup_insert($data) {
    $result = $this->db->insert('registration_detail', $data);
    $rows =  $this->db->affected_rows();
    if($rows>0){
    return $rows;
    }else{
      return FALSE;
    }
}
Run Code Online (Sandbox Code Playgroud)

阅读http://www.codeigniter.com/user_guide/database/helpers.html