使用一种形式将多行插入数据库 [英] Insert multiple rows into DB using one form
问题描述
我正在使用Codeigniter MVC框架,我试图在数据库中插入14行。另外,我的控制器中有14个模型调用。无论如何,它只在星期天早上插入。这是为什么?
*我发布的视图只是一小部分,每天(周一至周日)和(早晨,晚上)都会重复。
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:
+ --------- ++ --------- + -------- +
+---------+---------+--------+
|天时间| user_id |
| day | time | user_id|
+ --------- + --------- + -------- +
+---------+---------+--------+
|周日|早上1 |
| Sunday | morning | 1 |
+ --------- + --------- + -------- +
+---------+---------+--------+
|星期一|晚上1 |
| Monday | evening | 1 |
+ --------- + --------- + -------- +
+---------+---------+--------+
模型
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;
}
控制器
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);
}
查看
<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>
如果我打印$ data数组,这是输出:
This is the output if I print the $data array:
Array
(
[0] => Array
(
[date] => 2020-05-10
[day] => sunday
[time] => morning
[worker_name] => Victoria
)
)
推荐答案
您提交的所有输入帖子都是数组,但您只需在( 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);
}
}
这篇关于使用一种形式将多行插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!