Adrian Fischer
Adrian Fischer

Reputation: 119

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?

Upvotes: 1

Views: 3865

Answers (5)

user2598812
user2598812

Reputation:

You have not put html closing tag </option>. Use the following code:

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

Upvotes: 3

Hari Darshan
Hari Darshan

Reputation: 1920

You're not showing the text so the option values are not visible in select dropdown.

$areaOptions .= '<option value="'. $row['riskArea'].'">'; 

this should be like this

$areaOptions .= '<option value="'. $row['riskArea'].'">.'$row['riskArea']'.</option>';

Upvotes: 1

Pupil
Pupil

Reputation: 23948

Required Changes:

1) <option> text is not showing.

2) <option> is not closing.

Corrected Code:

//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'].'">' . $row['riskArea'] . '</option>';
}

Upvotes: 2

Saty
Saty

Reputation: 22532

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>';
}

Upvotes: 2

Dhara Parmar
Dhara Parmar

Reputation: 8101

You forgot to complete the option tag:

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

In select tag:

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

Upvotes: 2

Related Questions