douxianxing5712 2017-05-20 02:10
浏览 68

填充从数据库下拉的HTML表单

I have been trying to populate a drop down menu by pulling the needed information from a database through php and mysql however I haven't been able to achieve this and so far when I run it I just get a drop down box with nothing inside of it. Any help on this would be great as I am really stuck as to where I am going wrong.

<select id="dung-name-select" name="dung-select">
    <option name="" disabled selected hidden>Name</option>
    <option value="*">All</option>
        <?php 
        //Database Query
        $query = "SELECT Name FROM dungeon";
        $result = mysqli_query($connection, $query);
        if (!$result) {
            die("Database query failed.");
        }
            var_dump($result);
            while($row = mysqli_fetch_assoc($result)) {
        ?>
            <option value="<?= $row['Name']; ?>"><?= $row['Name']; ?></option>;
        <?php
        }
        ?>
</select>

I have connected to the database fine and can pull information down from it to populate a table for example however the drop down menu just isn't working.

NB: I need the value to be set to the name that I am also populating the drop down menu with.

  • 写回答

1条回答 默认 最新

  • dongmao3148 2017-05-20 03:08
    关注

    your code works for me, i tried something different hope it works for you.

    <select id="dung-name-select" name="dung-select">
        <option name="" disabled selected hidden>Name</option>
        <option value="*">All</option>
         <?php 
            $result = $connection->query("SELECT Name FROM dungeon");
            if ($result) {
                while($row = $result->fetch_assoc()) {
            ?>
                <option value="<?= $row['Name']; ?>"> <?= $row['Name']; ?></option>;
        <?php
                    }
            }       
            else {
                echo "No dungeons found"; 
                }
            ?>
    </select>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助