简体   繁体   中英

Inserting data into multiple tables with foreign key using CodeIgniter framework

I tried to insert data into multiple tables that have foreign key, using CodeIgniter.

here's my first table called koor_pen
no_koor ( primary ) | utm_y | utm_x | latit | longi

here's my second table called input_pen
no_form ( primary ) | kode_bps | no_obs | no_koor ( foreign ) | t_tanah | catatan


here's my controller

function c_submit(){
    $data = array(
        'no_form' => $this->input->post('noform'),
        'kode_bps' => $this->input->post('kodebps'),
        'no_obs' => $this->input->post('noobs'),
        'no_koor' => $this->input->post('nokoor'),
        'tanaman_u' => $this->input->post('tutama'),
        't_tanah' => $this->input->post('ttanah'),
        'catatan' => $this->input->post('cat')
    );

    $datakoor = array(
        'no_koor' => $this->input->post('nokoor'),
        'utm_y' => $this->input->post('y'),
        'utm_x' => $this->input->post('x'),
        'latit' => $this->input->post('deg')." ".
                    $this->input->post('min')." ".
                    $this->input->post('sec'),
        'longi' => $this->input->post('deg2')." ".
                    $this->input->post('min2')." ".
                    $this->input->post('sec2')
    );

    $no_obs = $this->session->userdata('no_obs');
    $this->m_input->m_submit($data, $datakoor);
    redirect(base_url("c_input"));
}

and the model

function m_submit($data, $datakoor) {

    $this->db->trans_start();

    $this->db->insert('koor_pen', $datakoor); 
    $no_koor = $this->db->insert_id(); 

    $this->db->where('no_koor',$no_koor);
    $this->db->insert('input_pen', $data);

    $this->db->trans_complete(); 

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

}

when I run the code, it shows an error like this 在此处输入图片说明

Your value is getting null. You have to pass $no_koor in $data so that value can be replaced. Try this:

function m_submit($data, $datakoor) {

    $this->db->trans_start();

    $this->db->insert('koor_pen', $datakoor); 
    $no_koor = $this->db->insert_id(); 

    //$this->db->where('no_koor',$no_koor);
    $data['no_koor'] = $no_koor;
    $this->db->insert('input_pen', $data);

    $this->db->trans_complete(); 

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

}

Problem is here no_koor (foreign) this is you foreign key and in your query no_koor this field is getting "" in your query as you send in image. so please check your query first.

The error is related to foreign key constraint as shown in your image. The rule is, you can only add or update a value in child table which are already present in parent table. So at the time of insertion make sure the value you are trying to insert in child table, already exist in parent table.

Sometimes the insertion sequence also matters, may be the value is their in the query, but you are running child table query first. So check the order also in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM