简体   繁体   English

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

[英]How to populate HTML dropdown list with values from database

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>

My guess is that you have a problem since you don't close your select-tag after the loop. 我的猜测是你遇到问题,因为你没有在循环后关闭你的select-tag。 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>

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>";
?> 

I'd suggest following a few debugging steps. 我建议遵循几个调试步骤。

First run the query directly against the DB. 首先直接针对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中找到了什么 - 看看它是否与你在DB中直接得到的相匹配。

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 eg 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手册

<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>
<?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>

Use OOP concept instead. 改为使用OOP概念。 Create a class with function 用函数创建一个类

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 完整的代码和说明可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM