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

使用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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doupu9251 2016-06-17 11:08

    You should select database in your file.Above you only connect to your localhost.And you should use variable name $rows for all fetch values

    if (!mysqli_select_db("mydbname")) {
        echo "Unable to select database: " . mysqli_error();
        exit;
    }
    
    评论
    解决 无用
    打赏 举报
  • douxian4376 2016-06-17 11:24
         // cat  is array fetch from database
    
         <select name="cat" id="cat_id" style="width:170px" value="<?php echo $_POST['cat']; ?>">
    
    
    <option value="0"></option>
        <?php
        foreach ($cat as $row) {
            ?>
                                                    <option value="<?php echo $row->tag_cat_id ?>" ><?php echo $row->tag_cat_name ?></option>
            <?php }
        ?>
                                            </select>
                                                </select>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题