php 从 MySQL 数据库中获取数据到 html 下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37077152/
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 01:03:11  来源:igfitidea点击:

Fetching data from MySQL database to html drop-down list

php

提问by Meet Bhatt

I search for answers here but haven't found a solution. I have also added picture of the error. I want the data to go to the first drop-down list ( above the error) I think the method I try to perform is also create drop-down list, am I correct?

我在这里搜索答案,但还没有找到解决方案。我还添加了错误的图片。我希望数据转到第一个下拉列表(错误上方)我认为我尝试执行的方法也是创建下拉列表,对吗?

           <form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
               <div class="form-group">
                   <label id="senderName">?? ?????:</label>



               </div>
            <div class="form-group">

                <label for="to_user">???:</label>
                <select name="to_user" class="form-control">
                    <option value="pick">??? ???????</option>
                        <?php

                        $sql = \mysqli_query("SELECT name From users");
                        $row = mysqli_num_rows($sql);


                            echo "<select name='to_user'>";
                            while ($row = mysqli_fetch_array($sql)){
                                echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
                            }
                            echo "</select>" ;

                        ?>

                </select>


            </div>

picture of the error

错误的图片

回答by The Codesee

In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \before the statement.

在 MySQLi 中,查询的第一个参数需要是数据库连接。此外,无需\在语句前添加 a 。

$sql = \mysqli_query("SELECT name From users");should be $sql = mysqli_query($con, "SELECT name From users");

$sql = \mysqli_query("SELECT name From users");应该 $sql = mysqli_query($con, "SELECT name From users");

Note: replace $con with your database connection variable!

注意:将 $con 替换为您的数据库连接变量!

As you mentioned that you wanted the result from the database to go inside the selectform, simply adjust your code to look like this:

正如您提到的,您希望将数据库中的结果放入select表单中,只需将代码调整为如下所示:

<select name="to_user" class="form-control">
<option value="pick">??? ???????</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>

回答by Raj Singh

<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">

<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>

</div>
</div>

回答by Meet Bhatt

<label><b>Select Steam: </b></label>
<select id="study">
    <option value="" selected="selected" disabled="">---Selected---</option>
    <?php
    $query = "SELECT study FROM details";
    $query_run = mysqli_query($con, $query);

    while ($row = mysqli_fetch_array($query_run)) {
        echo "<option value='".$row['study']."'>".$row['study']."</option>";
    }

?>
</select>