php - $this->db->insert_id();每次在 codeigniter 中返回 0

标签 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;
    }

这是我的 Controller 代码:

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

最佳答案

因此 $this->db->insert_id()

The insert ID number when performing database inserts.

而且你的数据库中没有自动递增的id

检查数据是否插入

$this->db->affected_rows()

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).

模型

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

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

关于php - $this->db->insert_id();每次在 codeigniter 中返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32655723/

相关文章:

javascript - 文件正在自动提交

php - Node.js 相当于 PHPlivedocx?

php - 用户名册,加入表但丢失数据

javascript - 数字插入数据库,但不插入字符或数字/字符

mysql - 如何从一个表中删除引用另一个表的所有行?

MySQL - 连接两个没有重复的表?

php - Android 应用程序与用代码点火器框架编写的 php 文件通信

php - 切换表时出现 zend db 错误可捕获的 fatal error : Argument 1 passed to __construct() must be an array, 给定对象,调用

PHP cURL 超时被忽略?

php - 关于优化连接查询的建议