如何将SQL表中的SQL数据回显为<select>表单?

时间:2019-07-17 02:22:51

标签: php mysql drop-down-menu select-options

我想将一些SQL数据echo转换为<select>形式,每个数据与表中的行不同。

我有一张桌子:-

repair (repair_id , date , machine_name ,Machine_vin , request , adresss);

我想在选择选项中加入Machine_vin号。.每次删除或添加一个,它都会反映在选项中。

我陷入了代码困境,只有:

    <?php 
       $sql = "select * from repair";
       $res = mysqli_query($conn,$sql);
       $selectResult="";
       while($list = mysqli_fetch_array($res)){
       $Machine_vin  = $list['Machine_vin'];

       $selectResult.="<option value="$Machine_vin">$Machine_vin</option>";

    ?>

获取解析错误:语法错误,意外的'$ Machine_vin'

我如何在machine_vins选项中反映<select>

3 个答案:

答案 0 :(得分:2)

<?php 
  $sql = "select * from repair";
  $res = mysqli_query($conn,$sql);
  $selectResult="";
  while($list = mysqli_fetch_array($res)){
    $Machine_vin  = $list['Machine_vin'];

    $selectResult.="<option value='$Machine_vin'>$Machine_vin</option>";

    ?>
<form method='post' action='#'>
<select name="course">
   <option value="0">Please Select Option</option>
      <?php echo $Machine_vin; ?>
</select>
</form>

答案 1 :(得分:1)

假设您是PHP和MySQL的新手 对于初学者,您可以开始阅读以下内容:

https://www.php.net/manual/en/book.mysqli.php

要获取数据并通过PHP进行解析,就像这样:

https://www.php.net/manual/en/mysqli-result.fetch-assoc.php

然后,您将使用输出缓冲区ECHO脚本进行播放,并呈现要放入其中的标签。

答案 2 :(得分:1)

我认为您应该使用尝试过的方法来更新您的问题。即使是一段代码,您也试图实现目标。当您需要fetch数据并将这些数据添加到下拉列表中时,可以按照以下步骤操作。

1。从数据库中获取数据。

2。使用while循环将每个数据分配到下拉选项中

    <div class="form-group">
    <label class="col-md-2 control-label">Machine_vin numbers </label>
    <div class="col-md-4">
        <select  class="form-control"  name="machine_vin" required>
            <option  value="0" selected disabled>Select the type of Machine vin number</option>
            <?php
             //fetch data from db
            $sql_machine_vin = ' SELECT * FROM  repair';
             // query the sql with db connection           
            $result_machine_vin = mysqli_query($conn,$sql_machine_vin);
             //loop the result
            while($row_machine_vin =mysqli_fetch_array($result_machine_vin)){
            ?>

             <option  value="<?=$row_machine_vin['Machine_vin'];?>" >
             <?=$row_machine_vin['Machine_vin'];?> ></option>
             <?php
            }
            ?>
        </select>
    </div>>
相关问题