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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效