dongzheng3113 2016-06-17 02: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 03: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条)
编辑
预览

报告相同问题?