我整天都在为此苦苦挣扎。
如何将insert_id()
第一次插入到模型中的另一个函数中使用?我尝试了会话,但我无法让它工作。
我什么都试过了。
我的第一个插入看起来像这样:
$aanbdata = array(
'Aanbieding' => $this->input->post('aanbiedingnaam'),
'Tekst' => $this->input->post('aanbiedingomschrijving'),
'Prijs' => $this->input->post('aanbiedingprijs'),
'Conditie' => $this->input->post('aanbiedingconditie'),
'prijssoort' => $this->input->post('prijsopties'),
);
$this->db->insert('Aanbiedingen', $aanbdata);
我试过这个:
session_start();
$this->session->set_userdata('aanbiedingid', $this->db->insert_id());
但是我的 id 在自动增量上落后 1,所以当我插入一行 id=4 时,会话是 id=3。
为什么?
编辑:
我的模型功能:
public function addaanbieding()
{
$aanbdata = array(
'Aanbieding' => $this->input->post('aanbiedingnaam'),
'Tekst' => $this->input->post('aanbiedingomschrijving'),
'Prijs' => $this->input->post('aanbiedingprijs'),
'Conditie' => $this->input->post('aanbiedingconditie'),
'prijssoort' => $this->input->post('prijsopties'),
);
$this->db->insert('Aanbiedingen', $aanbdata);
$this->addimages($image_data, $this->db->insert_id());
$catid = $this->input->post('categorie');
$bedrijfid = $this->session->userdata('idbedrijven');
$bedrijfaanb = array(
'idbedrijven' => $bedrijfid,
'idaanbiedingen' => $this->session->userdata('aanbiedingid'),
'idaanbiedingcat' => $catid
);
$this->db->insert('bedrijfaanbiedingen', $bedrijfaanb);
}
public function addimages($image_data)
{
$insert_data = array(
'fotonaam' => $image_data['file_name']
);
$this->db->insert('fotoaanbiedingen', $insert_data);
$fotoid = $this->db->insert_id();
$aanbiedingid = $this->session->userdata('aanbiedingid'); //this does nto work
$to_aanbiedingfotos = array(
'idaanbiedingen' => $aanbiedingid,
'idfotoaanbiedingen' => $fotoid
);
$this->db->insert('aanbiedingfotos', $to_aanbiedingfotos);
}