doutandusegang2961
doutandusegang2961
采纳率0%
2016-10-17 08:38

通过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 dongtan9518 5年前

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

    and in foreach

     $data[] = array(
     //ur data
       );
    
    点赞 评论 复制链接分享

相关推荐