0

I am struggling whole day with this.

How do I use the insert_id() from my first insert into another function in my model? I tried sessions, but I couldn't get it to work.

I tried everything.

My first insert looks like 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 my id runs 1 behind on the auto increment, so when I inserted a row with the id=4 then the session is 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 does nto work

    $to_aanbiedingfotos = array(
        'idaanbiedingen' => $aanbiedingid,
        'idfotoaanbiedingen' => $fotoid
    );

    $this->db->insert('aanbiedingfotos', $to_aanbiedingfotos);
}
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106

2 Answers2

1

You can do like 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 with your model name
$this->session->set_userdata('name', $last_id);

Model:

function add($data) {
   $this->db->insert('Aanbiedingen', $data);
   return $this->db->insert_id();
}
Erman Belegu
  • 4,074
  • 25
  • 39
0

Give the insert_id as a parameter to your model function.

Seriously, this is a very stupid answer. But I really don't know how to help you otherwise. What's keeping you from calling the "other model function" directly after you made the insert?

Like

$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->the_other_model->the_other_function($this->db->insert_id());
mgrueter
  • 1,400
  • 6
  • 15
  • How do I call the insert_id in the other model function?, because there's another insert statement – Kees Sonnema Jul 25 '13 at 14:16
  • Why would you want to do that? The other model has nothing to do with the insert from this model, so why should you call 'insert_id()' in the other model?` And since 'insert_id()' gives you back the last inserted id, you somewhat have to immediately call it right after the insert(). – mgrueter Jul 25 '13 at 14:23
  • Not another model. Another FUNCTION in my model. I will add both to my question so it's clear. – Kees Sonnema Jul 25 '13 at 14:26
  • just look at my question :) – Kees Sonnema Jul 25 '13 at 14:27
  • that really doesn't change anything. There is no reason to call 'insert_id()' in the other function. Just give it the return value if 'insert_id()' as a parameter. – mgrueter Jul 25 '13 at 14:29
  • I need the id from the first function to insert into the second. So how do I do that? – Kees Sonnema Jul 25 '13 at 14:31
  • well, how about returning it? in function addimages, after `$this->db->insert('aanbiedingfotos', $to_aanbiedingfotos);` add `return $this->db->insert_id();` and in function `addaanbieding` you'd have `$aabindig_id = $this->addimages($image_data, $this->db->insert_id());` – mgrueter Jul 25 '13 at 14:32
  • I don't get it. Isn't this in the wrong way? I need the id from addaanbieding in addimages. – Kees Sonnema Jul 25 '13 at 14:38
  • Dude, no idea. You keep calling your function 'the other', 'the seconde' etc. and I do my best to guess, but it's really hard with function names in another language ^^ – mgrueter Jul 25 '13 at 14:40
  • Sorry for that. I was really tired yesterday, so that's because of the annoying function names. – Kees Sonnema Jul 26 '13 at 06:37