2017-06-15 99 views
0

我尝试使用CodeIgniter将数据插入到具有外键的多个表中。

这是我的第一张表koor_pen
no_koor(小学) utm_y | utm_x | latit |隆基

这里是我的第二个表称为input_pen
no_form(主要)| kode_bps | no_obs | no_koor(国外)| t_tanah | catatan使用CodeIgniter框架将数据插入到具有外键的多个表中


这是我的控制器

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")); 
} 

和模型

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

} 

当我运行的代码,它显示了这样 enter image description here

+0

请检查您在主表中存在的主表中添加的值。 –

+0

如果两张表格在同一时间以一种形式输入会怎样? @PankajSharma – Kiki

+0

@Kiki你会在下面检查我的答案吗? –

回答

2

您的值变为空。您必须在$data中通过$no_koor,以便可以替换值。试试这个:

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

} 
0

的错误与foreign key constraint错误如图所示。规则是,您只能添加或更新child表中已存在于parent表中的值。因此在插入时请确保您试图在child表中插入的值已存在于parent表中。

有时插入顺序也很重要,可能值是它们在查询中,但是您首先正在运行子表查询。所以在这种情况下也要检查订单。

+0

如果两个表格在同一时间以一种形式输入会怎样? – Kiki

+0

该序列就像先在主数据库中添加数据,然后在子数据库中添加数据 –

+0

,这样当我在同一时间在同一时间输入两张表格时,是否可以使用? – Kiki

1

问题是在这里 no_koor(外国)这是你的外键和在你的查询no_koor这个字段越来越“”在您的查询,当你发送图像。所以请先检查您的查询。

相关问题