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

标签 php mysql codeigniter

基于insert multiple rows using one forigenk value in form

enter image description here

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

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

我如何传递其他字段的project_id。我的代码如下

Controller

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

        }

最佳答案

我认为您想要在 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。然后,您可以在每次插入详细信息表时使用它。

关于php - 为单个外键一次插入多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40058503/

相关文章:

php - 为什么我的查询不返回任何行?

php - 连接可能在另一表上没有匹配值的表

php - PHP Field Validation 使用什么方法

mysql - "SELECT"中的 "FROM"命令具有子 "SELECT"codeigniter

codeigniter - 使用 CodeIgniter 的空白屏幕

PHP/MYSQL延迟更新查询

php - Laravel 搜索工具不提供输出

php - 在表单提交上插入多条记录 - Yii

PHP。创建对象的最佳实践

html - 如何使用 <img src =""> 在 JSP 的系统目录中显示图像