doutandusegang2961 2016-10-17 08:38 采纳率: 0%
浏览 40
已采纳

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

according to insert multiple rows using one forigenk value in form

i want to insert multiple data at once through nested array. in my case data doesn't insert into the database. and no error messages to display.my code is below.

any one can please help me to slove this problem.

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

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

model

function create($data){
$this -> db-> insert_batch('boq',$data);
}
  • 写回答

1条回答 默认 最新

  • dongtan9518 2016-10-17 09:56
    关注

    Try like this Comment //$data=[];

    and in foreach

     $data[] = array(
     //ur data
       );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 串口控制LED灯驱动板设计
  • ¥15 如何在unity场景里做到通过按钮实现模型替换
  • ¥15 uniapp的websocket和c++的websocket服务器通讯 发送的数据和接受的数据表现形式不一样
  • ¥15 pyspark配置中的错误
  • ¥15 html word表格
  • ¥15 kali安装包如何解决,请给明确方案和步骤
  • ¥15 DEA软件进行企业创新效率的计算
  • ¥15 树莓派驱动42步进电机
  • ¥20 bevfusion mit可视化遇到的问题
  • ¥15 如何获得《人工智能工程技术人员》认证?