【问题标题】:insert multiple records at once for single foreign key一次为单个外键插入多条记录
【发布时间】:2016-10-15 11:33:28
【问题描述】:

基于insert multiple rows using one forigenk value in form

根据上图有选择项目的下拉菜单。一旦选择项目,用户将能够添加其他详细信息。现在我想在表单提交 project_id(下拉列表中带有项目名称的 id 映射)时应该插入其他行。
但在我的情况下 project_id 只插入第一行。当我打印数组时出现以下结果。

数组 ( [0] => 数组 ( [project_id] => 1 [staff_id] => 2 [item_no] => 1 [描述] => 1 [数量] => 1 [单位] => 立方 [率] => 1 [laboure_hrs] => 1 [laboure_cost] => 1 [数量] => 2) 1 => 数组 ( [project_id] => [staff_id] => 2 [item_no] => 2 [description] => 2 [数量] => 2 [单位] => sq.ft [费率] => 2 [laboure_hrs] => 2 [laboure_cost] => 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 ) )

我如何将 project_id 传递给其他字段。我的代码在下面

控制器

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

}

型号

function create($data){

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

        }

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    我认为您想在project 表中插入一行,然后使用project_id 列的值作为外键将几行插入到item 表中。

    (恕我直言,您的问题很难理解;如果您得到英语良好的人的帮助,您可能会得到比我更好的答案。)

    这就是你要做的。

    首先,在project 表中插入一行。不要在 INSERT 查询中提及 project_id 列,因此 MySQL 将分配一个自动递增的主键。

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

    那么,retrieve the value of the autoincrementing ID using LAST_INSERT_ID() 如下。

      SET @project_id := LAST_INSERT_ID();
    

    然后,使用该值将行插入到您的 item 表中。

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

    诀窍是在插入主表后立即检索LAST_INSERT_ID() 的值并将其存储在MySQL user-defined variable @project_id 中。然后将它用于每次插入详细信息表中。

    【讨论】:

    • project_id 不是主键。它是外键
    【解决方案2】:

    从您的 html 中,project_id 不是控件数组。所以你应该做的是在循环之前检索project_id并在循环中使用它或者在循环中删除project_id前面的[$i]

    【讨论】:

    • 先生,我已经按照您的说法编辑了我的代码。但是当我单击提交按钮时没有任何反应。我已经编辑了我的问题。你能帮帮我吗。
    • 将 $data=array('project_id' => $project_id) 更改为 $data=[] 并在创建数组的循环中放置 project_id' => $project_id 它应该可以工作跨度>
    • 伟大的先生。但我对模型有一些问题。但数据不会插入表中。我的模型有什么问题吗?
    • 这是我的型号代码,如下所示。函数创建($data){ $this->db->insert_batch('boq',$data); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2021-04-28
    • 2016-12-31
    • 2016-03-26
    • 2020-10-18
    相关资源
    最近更新 更多