【问题标题】:i can't get last insert id from database in codeigniter我无法从codeigniter中的数据库中获取最后一个插入ID
【发布时间】:2019-01-01 06:09:03
【问题描述】:

我想在 CodeIgniter 中创建一个 ID,例如 PTGS-1,其中 1 来自自动递增的列,PTGS 来自我创建的函数。

我创建了 2 列,1 列仅用于自动递增,1 列用于自定义 ID PTGS-1。并且每次我要插入数据,具有自定义 id 的列总是返回 PTGS-0,它不会得到 last insert id

这是我在模型中自定义 id 的函数

public function custIdPetugas() {

    $prefix = "PTGS" . "-";
    $lastid = $this->db->insert_id();

    $customid = $prefix.$lastid;

    return $customid;

}

以及模型中的这个函数来处理用户输入

public function simpan() {

    $custom_id = $this->custIdPetugas();
    $post = $this->input->post();

    $data = array(
        'custom_id' => $custom_id,
        'nama_petugas' => $post['nama'],
        'username' => $post['uname'],
        'password' => password_hash($post['pword'], PASSWORD_DEFAULT),
        'tgl_lahir' => $post['tgllahir'],
        'alamat' => $post['alamat'],
        'no_telpon' => $post['notelpon']
    );

    $this->db->insert($this->tbl, $data);

}

和控制器

public function tambahPetugas() {

    $petugas = $this->PetugasModel;
    $validation = $this->form_validation;
    $validation->set_rules($petugas->rules());

    if($validation->run()) {

        $petugas->simpan();
        $this->session->set_flashdata('berhasil', 'Data berhasil ditambah!');

    }

    $this->load->view('petugas/petugas-tambah');

}

问题在于自定义 id,我可以干净地将数据从表单插入数据库,但自定义 id 总是返回 0。

谢谢!

【问题讨论】:

  • 插入记录后可以得到最后插入ID。所以把这行代码$custom_id = $this->custIdPetugas();放在这行$this->db->insert($this->tbl, $data);之后
  • 我收到一个错误,未定义的变量 custom_id。因为它的过程,从第一行到最后一行。所以 $custom_id = $this->custIdPetugas();最后读入,并在此之前声明 custom_id 可能是错误的
  • 检查我的答案

标签: php codeigniter codeigniter-3


【解决方案1】:

在数据库中插入记录后获取最后插入ID的代码。

$this->db->insert($this->tbl, $data);
$custom_id = $this->custIdPetugas();

但是,如果您想在插入记录之前获取,请使用此。 假设你最后一个插入 ID 是 99,它会给你 100 作为回报

SELECT AUTO_INCREMENT
  FROM  INFORMATION_SCHEMA.TABLES
  WHERE TABLE_SCHEMA = 'database_name'
  AND   TABLE_NAME   = 'table_name';

在插入记录之前获取最后插入 ID 的另一种方法。

$last_id = SELECT MAX(id) FROM table;

下一条记录递增 1

型号

public function custIdPetugas() {

    $prefix = "PTGS" . "-";
    //Suppose last ID is 23 

    $lastid = $this->db->query('SELECT MAX(id) as max_id FROM table')->row();

    //$lastid = 23; Increment it by one for next unique value
    $customid = $prefix.$lastid->max_id;

    return $customid;

}

【讨论】:

  • 我可以把这个放在哪里?从 INFORMATION_SCHEMA.TABLES 中选择 AUTO_INCREMENT,其中 TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name';
  • 与 $this->db->query() ?可以举个例子吗?
  • 它的工作谢谢!!!!:))。但它可能在最大 id 时 +1?当我尝试将自动增量重置为 0 并输入第一条记录时,它将是没有 id 的“PTGS-”,在该记录之后,我再次输入,并将其工作到 PTGS-1
  • 如果您的第一条记录的 id 为 0。使用此代码 $lastid = $this->db->query('SELECT MAX(id) as max_id FROM table')->row() 获取 id 后,像这样 $new_increment_id = $lastid +1 将 id 增加 1
【解决方案2】:

我认为问题出在您的insert_id() 函数中。您需要从custom_id 拨打最新号码,然后将其递增。所以,首先调用最新的号码,我将它存储在名为insert_id的函数中:

public function insert_id()
{       
    //here is query for called max id, because u have 4 character before the number so use right instead max. 
    $sql= sprintf("SELECT MAX(RIGHT(custom_id,1)) AS key FROM your_tables_name"); 
    $qry= $this->db->query($sql);
    $row= $qry->row();
    //here is checking if the record is null
    $record= $row->key;
    if ($record== null) {
        $record = 0;
    }
    $record+= 1;

    return $record
}

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多