1

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 enter image description here

5
  • Please check value you are adding in child table that exist in your master table. Commented Jun 15, 2017 at 7:02
  • What if the two tables is inputted in the same time in one form? @PankajSharma Commented Jun 15, 2017 at 7:13
  • @Kiki will you check my answer below? Commented Jun 15, 2017 at 7:26
  • @Kiki plz first check in koor_pen table record is inserted or not. Commented Jun 15, 2017 at 7:30
  • it really work, thanks for your help @B.Desai Commented Jun 15, 2017 at 7:31

3 Answers 3

2

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

5 Comments

What if the two tables is inputted in the same time in one form?
The sequence is like Add data in master first and then in child
so it is possible or not when i inputted two table in the same time in one form?
Yes, just follow the above sequence
yes i did, but it doesn't work at all. can you help me by seeing my model?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.