gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:11:46 24 4
gpt4 key购买 nike

基于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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com