doujiexin1136 2019-03-27 21:30
浏览 59
已采纳

使用PHP从数据库发出填充选择框

Basically, I have 1 selection box at the top of my page, called cups. There are 8 teams assigned to each cup. Upon selecting a cup and hitting submit I want all of the teams associated in that cup to fill 8 selection boxes elsewhere on the page allowing the user to choose which team plays the other.

I've attempted many things, and I have got to the point where I can echo the teams, they simply aren't being echoed into the selection boxes in a different div on the same page

<input type="submit" name="submit" value="Generate" class="submit">
<?php
if (isset($_POST['submit'])) { //checking if submit button was clicked

    include_once 'action/dbcon.php';

    $cname = $_POST['cupname'];

    if (empty($cname)) {
        header("Location: ../table.php?field=empty"); //return them if fields are empty
        exit();
    } else {
        $sql = "SELECT * FROM teams WHERE cup_name='$cname'";
        $show_teams = mysqli_query($conn, $sql);

        if (!$show_teams) {
            echo "Could not load teams! " . "(" . mysqli_error($conn) . ")";
        }
        while ($team = mysqli_fetch_assoc($show_teams)) {
            echo $team["team_name"];
        }
    }
    echo "dank";
}
?>
</form>


    <div class="seed-container">
        <div class="column-1">
        <form class="seed-form">
            <select name="team" required>
            <option></option>
            </select>
            <select name="team" required>
            <option></option>
            </select>

So, I'm echoing the right results, just not as options in the selection boxes where I want them

  • 写回答

2条回答 默认 最新

  • dsp1836 2019-03-27 21:40
    关注

    most easy might be to echo them where they should appear:

    <select name="team" required>
    <?php while ($team = mysqli_fetch_assoc($show_teams)) { ?>
        <option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
    <?php } ?>
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计