如何使用插入id()与会话插入另一个表?

时间:2013-07-25 14:03:29

标签: php mysql codeigniter session insert

我整天都在苦苦挣扎。

如何使用我的第一次插入中的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);
}

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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());