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
       );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据