简体   繁体   English

使用CodeIgniter框架将数据插入带有外键的多个表中

[英]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. 我尝试使用CodeIgniter将数据插入具有外键的多个表中。

here's my first table called koor_pen 这是我的第一张桌子koor_pen
no_koor ( primary ) | no_koor( 主要 )| utm_y | utm_y | utm_x | utm_x | latit | latit | longi 隆吉

here's my second table called input_pen 这是我的第二个表,名为input_pen
no_form ( primary ) | no_form( 主要 )| kode_bps | kode_bps | no_obs | no_obs | no_koor ( foreign ) | no_koor( 外国 )| t_tanah | 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. 您必须在$data传递$no_koor ,以便可以替换该值。 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. 问题在这里no_koor(外来),这是您的外键,在您的查询no_koor中,当您发送图像时,此字段在查询中获得“”。 so please check your query first. 因此,请先检查您的查询。

The error is related to foreign key constraint as shown in your image. 该错误与图像中显示的foreign key constraint有关。 The rule is, you can only add or update a value in child table which are already present in parent table. 规则是,您只能在parent表中已经存在的child表中添加或更新值。 So at the time of insertion make sure the value you are trying to insert in child table, already exist in parent table. 因此,在插入时,请确保您要在child表中插入的值已经存在于parent表中。

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. 因此,在这种情况下,请检查订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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