dongzheng3113 2016-06-17 10:59
浏览 167
已采纳

使用PHP将数据库中的数据输出到下拉框中

I have a form designed with bootstrap style and I want to retrieve data from a database into a drop-down list. I tried this code, but I get a list with no text.

    <div class="form-group"><label class="col-sm-2 control-label">Location</label>
    <div class="col-sm-8"><select class="form-control m-b" name=Location>
    <?php
    $mysqli = new mysqli( 'localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db' );
    /* check connection */
    if (mysqli_connect_errno())
    {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
    }
    $query = "SELECT * FROM adhoc";
    $result = mysqli_query($mysqli,$query);
    while($rows = mysqli_fetch_assoc($result))
    {
    echo "<option value=" . $rows['Aim'] . "></option>";

    echo "<option value='{".$d['Aim']."}'>".$d['Aim']."</option>";
    }
?> 
  • 写回答

3条回答 默认 最新

  • doujiu9172 2016-06-17 11:05
    关注
    1. $d is never mentioned before you try to echo it, so what is it's content?
    2. echo "<option value=" . $rows['Aim'] . "></option>"; has two issues: firstly, the value is not encased in quotes, and secondly, there is no content in the tag, so no text will output.

    Try changing your while to:

    while($rows = mysqli_fetch_assoc($result)){
        echo "<option value='{$rows['Aim']}'>{$rows['Aim']}</option>";
    }
    

    As long as $rows['Aim'] contains the content you wish to output, this should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序