1

I have an SQL DB loaded with security questions and answers. I need to display them in an html dropdown. I thought I could populate the dropdown fields with a while loop but I can't get it to work.

Any idea what's wrong with this?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php

$conn = mysql_connect("localhost","root","")or die (mysql_error());

mysql_select_db("assignment_3", $conn);

$data = "select securityQuestion, securityAnswer from securityquestiontable";

$result = mysql_query($data, $conn) or die (mysql_error());

while ($row = mysql_fetch_assoc($result)) {
print "<select>";
print "<option value=""";
print "disabled="disabled" selected="selected">";
print "Please Select your Undergraduate School";
print "</option>";
print "<option value="1">";
print $row["securityQuestion"];
print "</option>";
print "<option value="2">";
print $row["securityAnswer"];
print "</option>";
print "</select>";
}

?>

</body>
</html>
1
  • Are you getting any rows from that query in phpmyadmin? Nov 18, 2013 at 2:02

4 Answers 4

3

You need the print statements outside the loop. You are creating a new menu each time the loop goes around. Try this:

print "<select>";
while ($row = mysql_fetch_assoc($result)) {
print "<option value=""";
print "disabled="disabled" selected="selected">";
print "Please Select your Undergraduate School";
print "</option>";
print "<option value="1">";
print $row["securityQuestion"];
print "</option>";
print "<option value="2">";
print $row["securityAnswer"];
print "</option>";
}
print "</select>";
1

Try something like this:

echo '<select>';
while ($row = mysql_fetch_assoc($result)) {
    echo '<option value="" disabled="disabled" selected="selected">Please Select your Undergraduate School</option>';
    echo '<option value="1">'.$row["securityQuestion"].'</option>';
    echo '<option value="2">'.$row["securityAnswer"].'</option>';
}
echo '</select>';

or

print "<select>";
while ($row = mysql_fetch_assoc($result)) {
    print "<option value=""";
    print "disabled="disabled" selected="selected">";
    print "Please Select your Undergraduate School";
    print "</option>";
    print "<option value="1">";
    print $row["securityQuestion"];
    print "</option>";
    print "<option value="2">";
    print $row["securityAnswer"];
    print "</option>";
}
print "</select>";

I put the select tags outside the loop.

0

You are closing your print by using " everywhere, try this

print "<option value='1'>".$row['securityQuestion']."</option>";

instead of

print "<option value="1">$row["securityQuestion"]</option>";

Same here, try this

print "<option value='' disabled='disabled' selected='selected'> Please Select your Undergraduate School </option>";

instead of

print "<option value="" disabled="disabled" selected="selected"> Please Select your Undergraduate School </option>";

And put your select outside your loop.

0

You have made some minor mistake

Please try this code..

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<?php

$conn = mysql_connect("localhost","root","")or die (mysql_error());

mysql_select_db("assignment_3", $conn);

$data = "select securityQuestion, securityAnswer from securityquestiontable";

$result = mysql_query($data, $conn) or die (mysql_error());
print "<select>";
while ($row = mysql_fetch_assoc($result)) {

print "<option value=""";
print "disabled="disabled" selected="selected">";
print "Please Select your Undergraduate School";
print "</option>";
print "<option value="1">";
print $row["securityQuestion"];
print "</option>";
print "<option value="2">";
print $row["securityAnswer"];
print "</option>";

}
print "</select>";
?>

</body>
</html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.