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

报告相同问题?

悬赏问题

  • ¥20 求帮,直连能连上oracle12,但是thinkphp6就是报错
  • ¥15 paddleocr运行报错
  • ¥15 怎么用 matlab 设计滞后-超前串联校正网络
  • ¥15 MFC引用C#生成的dll,将dll放置到非exe程序目录,如何操作
  • ¥15 C#创建webservice接口,三方通过多次跳转访问本方服务,获取wsdl文档,wsdl中ip地址为局域网内本机地址而非三方直接访问的地址。
  • ¥15 关于#wireshark#的问题:需要安卓app流量数据集要安卓流量做包序列长度的实验,比如某些流量是在看视频还是在发评论
  • ¥15 Smail语句如何使用判断语句跳过验证卡密界面
  • ¥15 关于#wireshark#的问题:并且能够给数据做标注,如这个流量是在看视频或者是在转账
  • ¥15 运筹优化,gurobi,python
  • ¥20 springmvc重定向和返回json