我尝试使用php创建一个由SELECT查询填充的下拉列表,但它不会下拉。这是我的PHP脚本
<?php
require_once ('mysqli_connect.php');
@mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = @mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
// while($row = @mysqli_fetch_array($result))
// {
// echo "<p>$row[0] $row[1]</p>\n"; //this works
// }
echo "<td bgcolor=\"#E6E6E6\"";
echo "<strong>Category:</strong>";
echo "<select name=\"category\">";
while($row = @mysqli_fetch_array($result))
{
// echo "<p><option value='".$row[0]."'>".$row[1]."</option></p>\n";
echo "<option value='".$row[0]."'>".$row[1]."</option>";
}
mysqli_close($dbc);
echo "</select>";
echo "</td>";
?>
查询工作(下拉列表外的注释while循环显示15条记录)。但下拉列表仅显示一个category_name,不会下拉。有人能帮我解决问题吗?感谢。
答案 0 :(得分:0)
您的代码存在一些问题。
您可以删除echo "<td bgcolor=\"#E6E6E6\"";
和echo "</td>";
这就是您的数据未显示的原因。
或者,添加相应的<table></table>
代码。
另外,echo "<td bgcolor=\"#E6E6E6\"";
echo "<td bgcolor=\"#E6E6E6\">";
应该是>
,因为<td>
已经丢失了. "\n"
。
因此,为了获得正确的表格语法,请执行以下操作:
旁注: 我添加了<?php
require_once ('mysqli_connect.php');
@mysqli_select_db($dbc,"test");
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = @mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
?>
,以便获得干净且整齐的HTML源代码。
<form id="form1" name="form1" method="post" action="form1_handler.php">
<?php
require_once ('mysqli_connect.php');
@mysqli_select_db($dbc,"test");
if(isset($_POST['submit'])){
$query = "SELECT category_id, category_name FROM forum_category ORDER BY category_name";
$result = @mysqli_query($dbc, $query);
if(!$result)
{
echo "query error: " . mysqli_error($dbc);
}
echo "<table>" . "\n";
echo "<tr>" . "\n";
echo "<td bgcolor=\"#E6E6E6\">" . "\n";
echo "<strong>Category:</strong>" . "\n";
echo "<select name=\"category\">" . "\n";
while($row= mysqli_fetch_array($result)){
echo "<option value='".$row[0]."'>".$row[1]."</option>" . "\n";
}
echo "</select>" . "\n";
echo "</td>" . "\n";
echo "</tr>" . "\n";
echo "</table>" . "\n";
mysqli_close($dbc);
} // brace for if(isset($_POST['submit']))
?>
<input type="submit" name="submit" value="Submit">
</form>
修改(试试这个),因为我不知道您的完整代码是什么样的。
{{1}}