Adrian Fischer Adrian Fischer - 7 months ago 11
HTML Question

Drop down list not displaying

I am populating my drop downs using mysql and php. I have the contents stored in a table. I call them at the head of a page where they are needed an then insert the results where needed. I am doing it this way so the end user can update their own drop down list from a UI. It also saves me having to type the bloody things over and over. I have it done alread for some drop down number list. Usual thing, select a number etc. Works a treat. This new one is for words. I know the sql is getting the data from the db and the drop down does actually drop down but you cant see anything on the page. View page source shows me that the data is in the page. Its just seeable. The code for embedding into the page is exactly the same as the numbered drop downs. Im stumped. I should point out that using code to populate the drop downs was taught to me by someone in this group. For which I am very grateful.

//POPULATE RISK AREA
$sql = "SELECT riskArea FROM tblriskarea ORDER BY riskArea ASC";
$result = $conn->query($sql);
if (!$conn->query($sql)) {
echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

$areaOptions = "";
while($row = $result->fetch_assoc()){
$areaOptions .= '<option value="'. $row['riskArea'].'">';
}


and the code to show it in the table is thus:

<select name="riskarea" style="width:100px" ><?php echo "$areaOptions"; ?></select></td>


but I get nothing. Anything leaping out at you?

Answer

Two error in your code

1)Your connection variable is $conn not $mysqli SO you can check your query error using

if (!$conn->query($sql)) {
echo "query failed: (" . $conn->errno . ") " . $conn->error;
}

2) Forget to write text and close the option in dropdown. It would be

$areaOptions = "";
while ($row = $result->fetch_assoc()) {
    $areaOptions .= '<option value="' . $row['riskArea'] . '">"' . $row['riskArea'] . '"</option>';
}