Insert multiple rows into DB using one form

拟墨画扇 提交于 2020-06-17 02:51:56

问题


I am using Codeigniter MVC framework, I am trying to insert 14 rows into DB. Also, I have 14 calls for the model in my controller. However somehow, it inserts only Sunday morning. Why is that? * The view I posted is just a small part that just repeats itself for each day (Monday-Sunday) and (Morning, Evening).

My DB looks like this:

+---------+---------+--------+

| day | time | user_id|

+---------+---------+--------+

| Sunday | morning | 1 |

+---------+---------+--------+

| Monday | evening | 1 |

+---------+---------+--------+

Model

public function saveFinalShifts($data) {

    $this->db->db_debug = FALSE;
    $error = NULL;
    if (!$this->db->insert_batch('final_shifts', $data)) {
        $error = $this->db->error();
    }
    return $error; 
}

controller

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

}

View

<div class="warp">    
<?php echo form_open('Shifts/savefinalShifts'); ?>
<table class="manage">
<th>
                <input type="date" name="date[]">
</th>
<td>
                <input type="hidden" value="sunday" name="day[]">
                <input type="hidden" value="norning" name="time[]">
                <?php
                foreach ($sunday_morning as $shift):?>
                <br><label><input class="get_value" type="checkbox" name="worker_name[]" value="<?php       echo $shift['fullname'];?>">
                    <?php echo $shift['fullname'];
                    ?></label>
                <?php  endforeach;  ?>
            </td>
<td>
                <input type="hidden" value="monday" name="day[]">
                <input type="hidden" value="morning" name="time[]">
                <?php
                foreach ($monday_morning as $shift):?>
                <br><label><input class="get_value" type="checkbox" name="worker_name[]" value="<?php echo $shift['fullname'];?>">
                    <?php echo $shift['fullname'];
                    ?></label>
                <?php  endforeach;  ?>
            </td>
</table>

    <input type="submit" class="submit-btn" value=”send">
<div>

This is the output if I print the $data array:

Array
(
    [0] => Array
        (
            [date] => 2020-05-10
            [day] => sunday
            [time] => morning
            [worker_name] => Victoria 
        )

)  

回答1:


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



回答2:


I think that the error exists because you are writing the query outside foreach loop in your controller function. I've written the corrected code below, see if this helps you.

 if(is_array($dates)){
    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); //Move this line inside foreach loop
        }
   }


来源:https://stackoverflow.com/questions/61597620/insert-multiple-rows-into-db-using-one-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!