php - 如何在单个提交中逐行插入值
问题描述
这是我的观点页面
这是动态字段测试名称和单位来自表我需要插入测试名称和结果以及正常值也单位。
我的控制器
$patient_id = $this->input->post('patient_id');
$doctor_id = $this->input->post('doctor_id');
$prescription_id = $this->input->post('prescription_id');
$lab_result =$this->input->post('lab_result');
$lab_test =$this->input->post('lab_test');
$units =$this->input->post('units');
$normal_value =$this->input->post('normal_value');
$cat_id = $this->input->post('cat_id');
for($i=0; $i<count($prescription_id); $i++)
{
$labreport[] = array(
'patient_id' => $patient_id[$i],
'doctor_id' => $doctor_id[$i],
'prescription_id' =>$prescription_id[$i],
'lab_result' => $lab_result[$i],
'lab_test' => $lab_test[$i],
'units' => $units[$i],
'normal_value' => $normal_value[$i],
'cat_id' => $cat_id, );
//echo '<pre>'; print_r($labreport); '</pre>'; exit;
}
$stringlabreport= json_encode($labreport);
$this->db->insert('patient_lab_report',$stringlabreport);
if($this->db->affected_rows()){
return true;
} else {
return false;
}
$this->session->set_flashdata('message','Lab Report Added Successfully');
redirect('laboratory_report/all');
=========================
这是我的查看代码
<tr>
<td><input type="hidden" value="<?php echo $data->name; ?>"name="lab_test[]"><?php echo $data->name; ?></td>
<td><input type="text" value="" name="lab_result[]" class="form-control"></td>
<td><input type="hidden" value="<?php echo $data->units; ?>" name="units[] "> <?php echo $data->units; ?></td>
<td><input type="hidden" value="<?php echo $data->n_value; ?>" name="normal_value[] "> <?php echo $data->n_value; ?></td>
</tr>
解决方案
使用数组名称作为输入。IE:
<form action="" method="post" name="someform">
<input name="row[0][test_name]"/>
<input name="row[0][result]"/>
<input name="row[1][test_name]"/>
<input name="row[1][result]"/>
<input type="submit" name="submitted" value="send">
</form>
因此,在提交时,在 php 中解析,使用filter_input
withFILTER_REQUIRE_ARRAY
作为选项:
<?php
$submitted = filter_input(INPUT_POST, 'submitted');
if(!empty($submitted)){
$rows = filter_input(INPUT_POST,'row', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
$parsed_rows = [];
foreach($rows as $row){
if(!empty($row['test_name']) && !empty($row['result'])){
$parsed_rows[] = $row;
}
}
}
?>
推荐阅读
- api - 有时我在使用 Microsoft Graph API 调用 /Users 端点时收到“DocumentDB Provider is not initialized”错误
- svelte - Swiperjs 子弹和箭不来 - Svelte,Sapper
- javascript - Javascript - 遍历 HTML 集合
- iot - 在 LoRaWAN 中支持 SOUTHBOUND
- c# - 无法将“System.Data.Entity.DbSet”类型的对象转换为 System.Collections.Generic.IEnumerable
- javascript - 异步角度请求的问题
- sql - 在谷歌分析中将纪元时间转换为人类可读的时间
- python - Pandas 中的 pivot_wider 是什么?
- javascript - 类型错误' ...在将js文件转换为tsx时与类型IntrinsicAttributes没有共同的属性
- excel - 将列移动到新工作表excel VBA