我有一个装有安全问题和答案的SQL DB。我需要在html下拉列表中显示它们。我以为我可以使用while循环填充下拉字段,但我无法使其工作。
知道这有什么问题吗?
<!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>
答案 0 :(得分:3)
您需要在外部打印语句。每次循环时,您都在创建一个新菜单。试试这个:
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 :(得分:1)
尝试这样的事情:
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>';
或
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>";
我把选择标签放在循环外面。
答案 2 :(得分:0)
您正在使用“无处不在,尝试此
来关闭您的打印件print "<option value='1'>".$row['securityQuestion']."</option>";
而不是
print "<option value="1">$row["securityQuestion"]</option>";
同样在这里,试试这个
print "<option value='' disabled='disabled' selected='selected'> Please Select your Undergraduate School </option>";
而不是
print "<option value="" disabled="disabled" selected="selected"> Please Select your Undergraduate School </option>";
将你的选择放在你的循环之外。
答案 3 :(得分:0)
你犯了一些小错误
请尝试此代码..
<!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>