0

我整天都在为此苦苦挣扎。

如何将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);
}
4

2 回答 2

1

你可以这样做:

控制器:

$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);

模型:

function add($data) {
   $this->db->insert('Aanbiedingen', $data);
   return $this->db->insert_id();
}
于 2013-07-25T14:21:39.820 回答
0

将 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);
$this->the_other_model->the_other_function($this->db->insert_id());
于 2013-07-25T14:13:48.763 回答