简体   繁体   English

一次为单个外键插入多个记录

[英]insert multiple records at once for single foreign key

based on insert multiple rows using one forigenk value in form 基于使用表单中的一个forigenk值插入多行

在此处输入图片说明

according to above picture there is dropdown for select project.once select project and user will be able to add other details. 根据上面的图片,选择项目有一个下拉菜单。选择项目后,用户将可以添加其他详细信息。 now i want to do when form is submitting project_id (id map with project name in drop down)should insert with other rows. 现在我想在表单提交project_id(下拉菜单中具有项目名称的id映射)时插入其他行。
but in my case project_id is inserting with only very first row. 但在我的情况下,project_id仅插入第一行。 below result came when i print the array . 当我打印数组时出现以下结果。

Array ( [0] => Array ( [project_id] => 1 [staff_id] => 2 [item_no] => 1 [description] => 1 [qty] => 1 [unit] => cube [rate] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [amount] => 2 ) 1 => Array ( [project_id] => [staff_id] => 2 [item_no] => 2 [description] => 2 [qty] => 2 [unit] => sq.ft [rate] => 2 [laboure_hrs] => 2 [laboure_cost] => 2 [amount] => 8 ) [2] => Array ( [project_id] => [staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] => cube [rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18 ) ) 数组([0] =>数组([project_id] => 1 [staff_id] => 2 [item_no] => 1 [描述] => 1 [数量] => 1 [单位] =>立方体[速率] => 1 [人工时间] => 1 [人工成本] => 1 [数量] => 2) 1 =>数组([project_id] => [staff_id] => 2 [item_no] => 2 [描述] => 2 [数量] => 2 [单位] =>平方英尺[费率] => 2 [劳力小时] => 2 [劳务成本] => 2 [金额] => 8)[2] =>数组([project_id] => [ staff_id] => 2 [item_no] => 3 [description] => 3 [qty] => 3 [unit] =>立方体[rate] => 3 [laboure_hrs] => 3 [laboure_cost] => 3 [amount] => 18))

how i can pass project_id for another fields. 我如何将project_id传递给另一个字段。 my code is below 我的代码如下

controler 控制者

public function create(){

//    validate fields
            $this->form_validation->set_rules('work_product_id', 'Work Product Id', 'required');
            $this->form_validation->set_rules('work_item_description', 'Work Item Description', 'required');
            $this->form_validation->set_rules('quantity', 'Quantity', 'required');
           $this->form_validation->set_rules('rate', 'Rate', 'required|numeric');
           $this->form_validation->set_rules('laboure_hrs', 'Laboure Hrs', 'required|numeric');
            $this->form_validation->set_rules('laboure_cost', 'Laboure Cost', 'required|numeric');

           if ($_POST) 
   {
        $project_id=$this->input->post('project');
        $staff_id=$this->input->post('staff_id');
        $item_no=$this->input->post('work_product_id');
        $description=$this->input->post('work_item_description');
        $qty=$this->input->post('quantity');
        $unit=$this->input->post('unit');
        $rate=$this->input->post('rate');
        $laboure_hrs=$this->input->post('laboure_hrs');
        $laboure_cost=$this->input->post('laboure_cost');
        $amount=$this->input->post('txtmultTotal');
           $data= [];

   for ($i = 0; $i < count($this->input->post('work_product_id')); $i++)
    {
       $data[$i] = array(
           'project_id' => $project_id
           'staff_id' => $staff_id[$i],
           'item_no' => $item_no[$i],
           'description' => $description[$i],
           'qty' => $qty[$i],
           'unit' => $unit[$i],
           'rate' => $rate[$i],
           'laboure_hrs' => $laboure_hrs[$i],
           'laboure_cost' => $laboure_cost[$i],
           'amount' => $amount[$i],
        );
       }
        print_r($data);
       $this->boq_model->create($data);
    }      

}

model 模型

function create($data){

     $this->db->insert_batch('boq',$data);

        }

I think you want to insert a single row into your project table, and then insert several rows into your item table using the value of your project_id column as a foreign key. 我想你想插入一行到你的project表,然后插入几行到您的item使用的值表project_id列作为外键。

(With respect, your question is hard to understand; you may get a better answer than mine if you get help editing it from someone with good English-language skills.) (相对而言,您的问题很难理解;如果您从英语水平较高的人那里得到编辑帮助,则可能会比我的问题得到更好的回答。)

Here's what you do. 这就是你要做的。

First, INSERT a row into your project table. 首先,在project表中插入一行。 Do not mention the project_id column in your INSERT query, so MySQL will assign an autoincrementing primary key. 不要在INSERT查询中提及project_id列,因此MySQL将分配一个自动递增的主键。

  INSERT INTO project (name, whatever) VALUES (?, ?);

Then, retrieve the value of the autoincrementing ID using LAST_INSERT_ID() as follows. 然后, 使用LAST_INSERT_ID()检索自动增量ID的值,如下所示。

  SET @project_id := LAST_INSERT_ID();

Then, insert rows into your item table using that value. 然后,使用该值将行插入到item表中。

 INSERT INTO item (project_id, description, qty, whatever)
           VALUES (@project_id, ?, ?, ?)

The trick is to retrieve the value of LAST_INSERT_ID() right after your master-table insertion and store it in the MySQL user-defined variable @project_id . 诀窍是在插入主表后立即检索LAST_INSERT_ID()的值,并将其存储在MySQL用户定义的变量 @project_id Then you use it for each insertion into your detail table. 然后,将其用于详细信息表中的每次插入。

From your html, the project_id is not a control array. 从您的html,project_id不是控件数组。 So what you should do is to retrieve the project_id before the loop and use it in the loop or remove the [$i] in front of the project_id in the loop 因此,您应该做的是在循环之前检索project_id并在循环中使用它,或者在循环中删除project_id前面的[$ i]

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

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