通过codeigniter中的嵌套数组将多行插入数据库

时间:2016-10-17 08:38:00

标签: php mysql codeigniter

根据insert multiple rows using one forigenk value in form

我想通过嵌套数组一次插入多个数据。在我的情况下,数据不会插入数据库。并且没有要显示的错误消息。我的代码如下。

任何人都可以帮我解决这个问题。

控制器

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

//            getting value of input fields


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

1 个答案:

答案 0 :(得分:0)

试试这样 评论//$data=[];

并在foreach

 $data[] = array(
 //ur data
   );