我正在使用 Codeigniter。我的表單有多行,每行具有相同的字段名稱。您能幫助我如何通過循環將其保存在數據庫中嗎?我收到錯誤錯誤號:1048 列“sn_unit”不能為空請看下面的代碼。預先感謝您的幫助。這是視圖<?php for ($i = 1; $i <= $number_to_check; $i++) {?><hr><div class="row"> <div class="col-xl-4"> <div class="mb-3"> <label class="form-label"><strong><?php echo $i;?></strong></label> <input type="text" class="form-control" name="sn_unit[]" placeholder="Unit Serial Number" required> </div> </div></div><?php }?>這是控制器for ($i = 1; $i <= count($this->input->post('sn_unit')); $i++) { $sn_unit = $this->input->post('sn_unit[$i]'); $chk = $this->auth->saveItemReport();}
1 回答

蠱毒傳說
TA貢獻1895條經驗 獲得超3個贊
您可以簡單地使用 foreach 循環遍歷所有值。
foreach($this->input->post('sn_unit') as $key => $value)
{
$sn_unit = $value;
$chk = $this->auth->saveItemReport();
}
如果您仍然喜歡使用 for 循環;永遠記住數組從 0 索引開始,所以:
$units = $this->input->post('sn_unit');
for ($i = 0; $i < count($units); $i++) {
$sn_unit = $units[$i];
$chk = $this->auth->saveItemReport();
}
- 1 回答
- 0 關注
- 90 瀏覽
添加回答
舉報
0/150
提交
取消