简体   繁体   中英

How to get/return last inserted id in CodeIgniter?

i have question. I have 2 tabel, name is 'tbl_pendidikan' and 'tbl_datapribadi'. And i have Model, name is M_pascasarjana.php

function pendidikan($table,$data) {
  $this->db->select_max('id_pendaftar');
  $get_id = $this->db->get('tbl_datapribadi');
  $query = $this->db->insert($table,$data);

} //END FUNCTION//

This is my controller

foreach($_POST['user'] as $user) {
    $this->m_pascasarjana->pendidikan('tbl_pendidikan', $user);
  }

I try to get last id from table ' tbl_datapribadi '. How to insert last id to table 'tbl_pendidikan'?

function pendidikan($table,$data) {
    $last_row = $this->db->select('id_pendaftar')->order_by('id_pendaftar',"desc")->limit(1)->get('tbl_datapribadi')->row()->id_pendaftar;
    $data['column_name'] = $last_row;
    $query = $this->db->insert($table,$data);
    return $this->db->insert_id();
} 

$last_row will hold the last id present in tbl_datapribadi table. column_name will be your column name for last inserted id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM