php 如何使用数据库中的值填充 HTML 下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8022353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:46:54  来源:igfitidea点击:

How to populate HTML dropdown list with values from database

phpsqlwhile-loop

提问by Bernard

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.

作为我正在创建的 HTML 表单的一部分,我想要一个下拉列表,该列表将列出我的数据库中的所有用户名。

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>

回答by Christofer Eliasson

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>

回答by Imon

Below code is nice.. It was given by somebody else named aaronbd in this forum

下面的代码很好..它是由本论坛中名为 aaronbd 的其他人提供的

<?php

$conn = new mysqli('localhost', 'username', 'password', 'database') 
or die ('Cannot connect to db');

    $result = $conn->query("select id, name from table");

    echo "<html>";
    echo "<body>";
    echo "<select name='id'>";

    while ($row = $result->fetch_assoc()) {

                  unset($id, $name);
                  $id = $row['id'];
                  $name = $row['name']; 
                  echo '<option value="'.$id.'">'.$name.'</option>';

}

    echo "</select>";
    echo "</body>";
    echo "</html>";
?> 

回答by Kris C

I'd suggest following a few debugging steps.

我建议遵循一些调试步骤。

First run the query directly against the DB. Confirm it is bringing results back. Even with something as simple as this you can find you've made a mistake, or the table is empty, or somesuch oddity.

首先直接对数据库运行查询。确认它正在返回结果。即使是这样简单的事情,你也会发现你犯了一个错误,或者桌子是空的,或者一些奇怪的事情。

If the above is ok, then try looping and echoing out the contents of $row just directly into the HTML to see what you've getting back in the mysql_query - see if it matches what you got directly in the DB.

如果以上没有问题,那么尝试循环并将 $row 的内容直接回显到 HTML 中,以查看您在 mysql_query 中返回的内容 - 查看它是否与您直接在数据库中获得的内容匹配。

If your data is output onto the page, then look at what's going wrong in your HTML formatting.

如果您的数据输出到页面上,那么请查看您的 HTML 格式中出了什么问题。

However, if nothing is output from $row, then figure out why the mysql_query isn't working e.g. does the user have permission to query that DB, do you have an open DB connection, can the webserver connect to the DB etc [something on these lines can often be a gotcha]

但是,如果没有任何输出$row,那么找出 mysql_query 不工作的原因,例如用户是否有权查询该数据库,您是否有一个开放的数据库连接,网络服务器是否可以连接到数据库等 [这些行上的某些内容通常可能是一个陷阱]

Changing your query slightly to

将您的查询稍微更改为

$sql = mysql_query("SELECT username FROM users") or die(mysql_error());  

may help to highlight any errors: php manual

可能有助于突出任何错误:php 手册

回答by user2806221

<select name="owner">
<?php 
$sql = mysql_query("SELECT username FROM users");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['username'] . "</option>";
}
?>
</select>

回答by rashedcs

<?php
 $query = "select username from users";
 $res = mysqli_query($connection, $query);   
?>


<form>
  <select>
     <?php
       while ($row = $res->fetch_assoc()) 
       {
         echo '<option value=" '.$row['id'].' "> '.$row['name'].' </option>';
       }
    ?>
  </select>
</form>

回答by tim3in

Use OOP concept instead. Create a class with function

请改用 OOP 概念。用函数创建一个类

class MyClass {

...

function getData($query) {
    $result = mysqli_query($this->conn, $query);
    while($row=mysqli_fetch_assoc($result)) {
        $resultset[] = $row;
    }       
    if(!empty($resultset))
        return $resultset;
} }

and then use the class object to call function in your code

然后使用类对象调用代码中的函数

<?php 

    $obj = new MyClass();
    $row = $obj->getData("select city_name from city"); 
?>
<select>
    <?php foreach($row as $row){ ?>
        <option><?php echo $row['city_name'] ?></option>

<?php  } ?>
</select>

Full code and description can be found here

完整的代码和描述可以在这里找到