I have a form which fetches data from a DB and inserts it to a database, some values are inserted blank even though I actually set its values not to be blank and even the browser output shows there is no value to
Here is my code:
$search_disease = $_POST['tdisease'];
$disease = "(select * from medications WHERE diagnosis='$search_disease')";
foreach ($pdo->query($disease) as $row){
$diagnosis = $row['diagnosis'];
$icd = $row['ICD10'];
}
i then echo the results to a select tag
<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">
And this is what the browser outputs:
<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>
What is the issue here?
You are echoing the diagnosis name but you are not echoing the ICD10 code in the value
attribute of the option
.
You should just do this: <option value="<?= !empty($row['ICD10'])?$row['ICD10']:'';?>"><?= $row['diagnosis'];?> </option>
The problem really is that in your while ($row=$result->fetch_array(MYSQLI_ASSOC))
loop, there is no assignment to the $icd
variable.
OPTION 2: Change your first php code above to:
$diagnoses = [];
foreach ($pdo->query($disease) as $row){
$diagnosis = [];
$diagnosis['diagnosis'] = $row['diagnosis'];
$diagnosis['icd10'] = $row['ICD10'];
$diagnoses[] = $diagnosis;
}
Then in the iteration part:
<?php foreach ($diagnoses as $disease) { ?>
<option value="<?= !empty($disease['icd10'])?$disease['icd10']:'';?>"><?= $disease['diagnosis'];?> </option>
<?php } ?>