Php 我无法从codeigniter中的数据库中获取最后一个插入id
我想在CodeIgniter中创建一个id,比如PTGS-1,其中1来自具有自动递增的列,PTGS来自我创建的函数 我创建了两列,一列用于自动递增,另一列用于自定义id PTGS-1。每次我必须插入一个数据,具有自定义id的列总是返回PTGS-0,它不会得到最后一个插入id 这是我在模型中自定义id的函数Php 我无法从codeigniter中的数据库中获取最后一个插入id,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我想在CodeIgniter中创建一个id,比如PTGS-1,其中1来自具有自动递增的列,PTGS来自我创建的函数 我创建了两列,一列用于自动递增,另一列用于自定义id PTGS-1。每次我必须插入一个数据,具有自定义id的列总是返回PTGS-0,它不会得到最后一个插入id 这是我在模型中自定义id的函数 public function custIdPetugas() { $prefix = "PTGS" . "-"; $lastid = $this->db->in
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的代码
$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
模型
我认为问题在于你的insert_id函数。您需要的是从您的自定义_id调用最新的号码,然后将其递增。因此,首先要调用最新的号码,我将其存储在名为insert_id的函数中:
您可以在插入记录后获取最后一个插入ID。所以把这行代码$custom_id=$this->custIdPetugas;在这一行之后,$this->db->插入$this->tbl,$data;我得到一个错误,未定义的变量custom_id。因为它的过程是从第一行到最后一行的。所以$custom_id=$this->custIdPetugas;阅读最后一段,并在此之前声明自定义\u id。检查我的回答。我可以把它放在哪里?从INFORMATION_SCHEMA.TABLES中选择AUTO_INCREMENT,其中TABLE_SCHEMA='database_name'和TABLE_name='TABLE_name';使用$this->db->query?你能举个例子吗?它的工作非常感谢!!!:。但是在最大id时+1是可能的吗?当我尝试将自动增量重置为0,并输入第一条记录时,它将是没有id的“PTGS-”,在该记录之后,我再次输入,如果您的第一条记录id为0,它将工作到PTGS-1。在获得代码为$lastid=$this->db->query'selectmaxid as max_id FROM table'->行增量id乘以1后,如$new_increment_id=$lastid+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;
}
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
}