Codeigniter在插入具有单个查询的3个表时未显示正确的insert_id

时间:2016-01-07 20:41:41

标签: php codeigniter codeigniter-3

我有这个应用程序,我用一个查询将数据插入3个表但看起来第二个表是从第一个表继承的正确id,但第三个表总是显示少一个(id-1)。例如。如果id为372,则显示371。

请建议

型号:

function insert_property_details($data) {

    // First Table
    $insert_property_in_database=array(
       'v_item_title'   => $this->input->post('v_item_title'),
       'v_item_category'=> $this->input->post('v_item_category'),
       'v_num_rooms'    => $this->input->post('v_num_rooms'),
       );
       $query=$this->db->insert('vbc_vacation_item_attri',$insert_property_in_database);

    // Second Amenity table
    $insert_property_amenities=array(
       'v_ref_id'       => $this->db->insert_id(),
       'v_aircondition' => $this->input->post('v_aircondition'),
       'v_handy'        => $this->input->post('v_handy'),
       );
       $query=$this->db->insert('vbc_property_amenities',$insert_property_amenities);

    // Third Image table
    $insert_images = array(
        'property_images' => $data['uploadedimage'],
        'property_ref_id'   => $this->db->insert_id(),
        );
        $this->db->insert('vbc_property_images', $insert_images);

        return $this->db->insert_id();
    }

1 个答案:

答案 0 :(得分:1)

只有一个insert_id(),然后使用变量来获取它也不需要$query进行插入。

型号:

function insert_property_details($data) {

    // First Table

    $insert_property_in_database = array(
        'v_item_title' => $this->input->post('v_item_title'),
        'v_item_category'=> $this->input->post('v_item_category'),
        'v_num_rooms' => $this->input->post('v_num_rooms'),
    );

    $id = $this->db->insert_id();
    $this->db->insert('vbc_vacation_item_attri',$insert_property_in_database);

    // Or have the id just below first insert
    // $id = $this->db->insert_id();

    // Second Amenity table

    $insert_property_amenities = array(
        'v_ref_id' => $id,
        'v_aircondition' => $this->input->post('v_aircondition'),
        'v_handy' => $this->input->post('v_handy'),
    );

    $this->db->insert('vbc_property_amenities',$insert_property_amenities);

    // Third Image table

    $insert_images = array(
        'property_images' => $data['uploadedimage'],
        'property_ref_id'=> $id,
    );

    $this->db->insert('vbc_property_images', $insert_images);
}