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

时间:2016-05-06 16:22:29

标签: php

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

           <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

3 个答案:

答案 0 :(得分:2)

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

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

注意:用数据库连接变量替换$ con!

正如您所说,您希望数据库中的结果进入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>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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