我有一个从数据库中获取数据并将其插入数据库的表单,即使我实际上将其值设置为空白,甚至浏览器输出显示没有值
这是我的代码:
$search_disease = $_POST['tdisease'];
$disease = "(select * from medications WHERE diagnosis='$search_disease')";
foreach ($pdo->query($disease) as $row){
$diagnosis = $row['diagnosis'];
$icd = $row['ICD10'];
}
然后我将结果回显给选择标记
<select style="width: 40%; margin-left: -1%; position: relative;top: -52px; left: 15px;" name="tdisease">
<option value="">Select Disease1</option>
<?php while ($row=$result->fetch_array(MYSQLI_ASSOC)) { ?>
<option value="<?php echo !empty($icd)?$icd:'';?>"><?php echo $row['diagnosis'];?> </option>
<?php } ?>
</select>
<input class="btn btn-warning" style="width: 170px; margin-left: 17%; margin-top: 15%; margin-bottom: 1%; color: #000;" type="submit" name="insertData" value="Create Invoice">
这就是浏览器输出的内容:
<select style="width: 40%; margin-left: -1%; position: relative;top: -52px; left: 15px;" name="tdisease">
<option value="">Select Disease1</option>
<option value="">Cholera due to Vibrio cholerae 01, biovar cholerae</option>
<option value="">Cholera due to Vibrio cholerae 01, biovar eltor</option>
<option value="">Cholera, unspecified</option>
</select>
这里有什么问题?
答案 0 :(得分:0)
您正在回显诊断名称,但您没有在value
的{{1}}属性中回显ICD10代码。
你应该这样做:
option
问题实际上是在
<option value="<?= !empty($row['ICD10'])?$row['ICD10']:'';?>"><?= $row['diagnosis'];?> </option>
循环中,没有while ($row=$result->fetch_array(MYSQLI_ASSOC))
变量的赋值。
方案2: 将上面的第一个PHP代码更改为:
$icd
然后在迭代部分:
$diagnoses = [];
foreach ($pdo->query($disease) as $row){
$diagnosis = [];
$diagnosis['diagnosis'] = $row['diagnosis'];
$diagnosis['icd10'] = $row['ICD10'];
$diagnoses[] = $diagnosis;
}