且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用一种形式将多行插入数据库

更新时间:2023-12-04 13:06:04

您提交的所有输入帖子都是数组,但您只需在( day,time,worker_name )和 [] ,例如 $ this-> input-> post( 'day []');
尝试删除 [] ,此外,移动呼叫模型 saveFinalShifts()在if语句中,以确保您只是在 $ dates 为数组(或不为空)时调用模型。

All input post you're submited is array, but you just pick first element of post data in (day, time, worker_name) with [] like $this->input->post('day[]');. Try to remove [] and in additional, move your calling model saveFinalShifts() inside if statement to sure you just calling model when $dates is array (or not empty).

public function saveFinalShifts(){  
    $data = array();
    $dates = $this->input->post('date');
    $days = $this->input->post('day');
    $times = $this->input->post('time');
    $worker_names = $this->input->post('worker_name');

    if(is_array($dates) && !empty($dates)){  // make sure $dates is array and not empty []
       foreach ($dates as $key => $date){
          $data[] =  array (
            'date' => $date,
            'day' => $days[$key],
            'time' => $times[$key],
            'worker_name' => $worker_names[$key],
          );
       }
       $this->Shifts_model->saveFinalShifts($data);
    }
}