我需要你的帮助。 我想同时使用insert_id和两个插入数据表。 enter image description here
function add_donhang(){
$this->db->insert('donhang', array(
'tendonhang' => $this->input->post('txttendonhang'),
'madonhang' => $this->input->post('txtmadonhang'),
'idkhachhang' => $this->input->post('idkhachhang'),
)); // after insert table "donhang" and at the same time insert into "phieuxuatkho"
$this->db->insert('phieuxuatkho', array(
'tendonhang' => $this->input->post('txttendonhang'),
));
$id = $this->db->insert_id(); // Get id of "donhang".
$id2 = $this->db->insert_id(); // Get if of "phieuxuatkho".
if ($id) {
$dulieusp = array();
$dsmasp = $this->input->post('masp');
$dstensp = $this->input->post('tensp');
$dsslsp = $this->input->post('soluong');
$dsmsp = $this->input->post('midsp');
foreach ($dsmasp as $key => $item) {
$dulieusp[] = array(
'masanpham' => $item,
'tensanpham' => $dstensp[$key],
'madonhang' => $id, //id of "donhang".
'maphieuxuat' => $id2, //if of "phieuxuatkho".
'idsanpham' => $dsmsp[$key],
'soluong' => $dsslsp[$key],
);
}
if (count($dulieusp)) {
$this->db->insert_batch('chitietdonhang', $dulieusp);
}
return array(
'type' => 'successful',
'message' => ' Thêm Đơn Hàng Thành Công'
);
} else {
return array(
'type' => 'error',
'message' => ' Không Có Đơn Hàng Thêm Vào'
);
}
}
我怎么能用简单的方法做到这一点。谢谢你的帮助