php - How to use the insert_id() with session to insert into other table? -
i struggling whole day this.
how use insert_id()
first insert function in model? tried sessions, couldn't work.
i tried everything.
my first insert looks this:
$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);
i tried this:
session_start(); $this->session->set_userdata('aanbiedingid', $this->db->insert_id());
but id runs 1 behind on auto increment, when inserted row id=4 session id=3.
why?
edit:
my model functions:
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 nto work $to_aanbiedingfotos = array( 'idaanbiedingen' => $aanbiedingid, 'idfotoaanbiedingen' => $fotoid ); $this->db->insert('aanbiedingfotos', $to_aanbiedingfotos); }
you can this:
controller:
$aanbdata['aanbieding'] = $this->input->post('aanbiedingnaam'); $aanbdata['tekst'] = $this->input->post('aanbiedingomschrijving'); $aanbdata['prijs'] = $this->input->post('aanbiedingprijs'); $aanbdata['conditie'] = $this->input->post('aanbiedingconditie'); $aanbdata['prijssoort'] = $this->input->post('prijsopties'); $last_id = $this->model_name->add($aanbdata); //change model_name model name $this->session->set_userdata('name', $last_id);
model:
function add($data) { $this->db->insert('aanbiedingen', $data); return $this->db->insert_id(); }
Comments
Post a Comment