I'm trying to get the ID of my master table to be inserted to my transaction table. The insert is happening inside a loop.
Behavior:
Controller:
//Insert Master
foreach($arr_master as $res_master){
/*$data['ID_REQUIREMENT_TRANS'] this is an auto-increment column. Inserted automatically*/
$data['ID_REQUIREMENT'] = $res_master;
$data['SENT_BY_DATE'] = date("Y-m-d H:i:s");
$data['STATUS'] = 1;
$this->MAdmin->ins_assign_pic($data);
//Insert Trans
foreach($arr_trans as $res_trans){
$data_d['ID_REQUIREMENT_TRANS'] = $id; //Need to get the last inserted id of the master table
$data_d['RECEIVED_BY'] = $res_trans;
$data_d['RECEIVED_BY_DATE'] = NULL;
$data_d['STATUS'] = 1;
$this->MAdmin->ins_assign_pic_d($data_d);
}
}
Model:
function ins_assign_pic($data){
$q_ins_assign_pic = $this->db->insert('pm_requirement_assign_pic',$data);
return $q_ins_assign_pic;
$id = $this->db->insert_id();
return $id; //How to send this to my controller
return true;
}
function ins_assign_pic_d($data_d){
$q_ins_assign_pic_d = $this->db->insert('pm_requirement_assign_pic_d',$data_d);
return $q_ins_assign_pic_d;
}
Try controller
$id = $this->MAdmin->ins_assign_pic($data);
And model
function ins_assign_pic($data){
$q_ins_assign_pic = $this->db->insert('pm_requirement_assign_pic',$data);
//return $q_ins_assign_pic;
$id = $this->db->insert_id();
return $id; //How to send this to my controller
// return true;
}
Way 1
SELECT MAX( id ) FROM tableName;
Way 2
SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;
You are doing little wrong here. once return
call in function then it will not execute further.
Model:
function ins_assign_pic($data){
$q_ins_assign_pic = $this->db->insert('pm_requirement_assign_pic',$data);
if($q_ins_assign_pic){
return $this->db->insert_id();
} else {
retun false;
}
}
Controller :
$newID = $this->MAdmin->ins_assign_pic($data);
您可以从表中获取最大ID:
select max(id) from table
mysqli_insert_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.