如何使用数据库中的值填充 HTML 下拉列表
作为我正在创建的 HTML 表单的一部分,我想要一个下拉列表,该列表将列出我数据库中的所有用户名.
as part of a HTML form I am creating I would like to have a dropdown list which will list all the usernames in my database.
我认为以下代码可以解决问题,但下拉列表是空的 - 有人可以帮助我解决我做错了什么吗?谢谢.
I thought the following code would do the trick but the dropdown list is empty - could someone assist me in what i'm doing wrong? Thanks.
<tr>
<td>Owner</td>
<td>
<select name="owner">
<?php
$sql = mysqli_query($connection, "SELECT username FROM users");
while ($row = $sql->fetch_assoc()){
?>
<option value="owner1"><?php echo $row['username']; ?></option>
<?php
// close while loop
}
?>
</td>
</tr>
推荐答案
我猜你有问题,因为你没有在循环后关闭你的选择标签.这能解决问题吗?
My guess is that you have a problem since you don't close your select-tag after the loop. Could that do the trick?
<select name="owner">
<?php
$sql = mysqli_query($connection, "SELECT username FROM users");
while ($row = $sql->fetch_assoc()){
echo "<option value="owner1">" . $row['username'] . "</option>";
}
?>
</select>
相关文章