如何从选择下拉列表中的数据库中获取数据

发布于 2025-01-15 14:12:48 字数 1406 浏览 0 评论 0原文

我正在尝试构建一个宾果游戏,我想在选择(下拉列表)中显示我以前的记录!如何从选择下拉列表中的数据库中获取数据 ---下面是我的php文件

           <html>
            <style>
                #rec_mode{
                    background-image: url('register.png');
                    background-size: 100% 100%;
                    width: 100px;
                    height: 50px;
                    border: none;
                    outline: 0px;
                    -webkit-appearance: none;  
                }
            </style>
            <body>
                <select name = "select_history" id="rec_mode">
                    <option selected="true" disabled="disabled">
                    <?php

                        require_once 'config.php';

                        // $hist = mysqli_query($mysqli, "SELECT name FROM `movie_names` ORDER BY movieID DESC");
                        $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
                        while ($row = $hist->fetch_assoc())
                        {
                            echo "<option value=\"select_history\">".$row['name']."</option>";
                        }
                    ?>
                    </option>
                </select>
            </body>
            </html>

I am trying to build a Bingo game, where I want to display my previous records in a select(dropdown)!! how do I fetch my data from database in select dropdown list
---below is my php file

           <html>
            <style>
                #rec_mode{
                    background-image: url('register.png');
                    background-size: 100% 100%;
                    width: 100px;
                    height: 50px;
                    border: none;
                    outline: 0px;
                    -webkit-appearance: none;  
                }
            </style>
            <body>
                <select name = "select_history" id="rec_mode">
                    <option selected="true" disabled="disabled">
                    <?php

                        require_once 'config.php';

                        // $hist = mysqli_query($mysqli, "SELECT name FROM `movie_names` ORDER BY movieID DESC");
                        $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
                        while ($row = $hist->fetch_assoc())
                        {
                            echo "<option value=\"select_history\">".$row['name']."</option>";
                        }
                    ?>
                    </option>
                </select>
            </body>
            </html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┾廆蒐ゝ 2025-01-22 14:12:48

为什么在 php 代码后面添加选项结束标记。不应该是这样的吗。

<select name = "select_history" id="rec_mode">
    <option selected="true" disabled="disabled">Select Option</option>
    <?php
       require_once 'config.php';
       $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
       while ($row = $hist->fetch_assoc()){
               echo "<option value=\"select_history\">".$row['name']."</option>";
        }
    ?>
</select>

Why have you added option end tag after php code. Isn't it supposed to be like this.

<select name = "select_history" id="rec_mode">
    <option selected="true" disabled="disabled">Select Option</option>
    <?php
       require_once 'config.php';
       $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
       while ($row = $hist->fetch_assoc()){
               echo "<option value=\"select_history\">".$row['name']."</option>";
        }
    ?>
</select>
数理化全能战士 2025-01-22 14:12:48

首先从数据库中选择旧的选定名称,例如“哈利·波特”
然后使用 if 条件在 while 循环中选择旧的,

    $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN 
host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
$Bingo = "harry potter"; // previously selected value 
                        while ($row = $hist->fetch_assoc())
                        {
                            $selected = "";
                            if($row["name"]==$Bingo) $selected = "selected"; // If you given Id use colunm name id
                            echo "<option value=\"select_history\" $selected >".$row['name']."</option>";
                        }

谢谢

First select old selected name from DB eg "harry potter"
then use if condition to select inside while loop selected for old one

    $hist = mysqli_query($mysqli,"SELECT m.name FROM movie_names m INNER JOIN 
host_table ht WHERE m.movieID = ht.random_num ORDER BY ID DESC");
$Bingo = "harry potter"; // previously selected value 
                        while ($row = $hist->fetch_assoc())
                        {
                            $selected = "";
                            if($row["name"]==$Bingo) $selected = "selected"; // If you given Id use colunm name id
                            echo "<option value=\"select_history\" $selected >".$row['name']."</option>";
                        }

Thank You

静谧 2025-01-22 14:12:48

首先,您必须正确关闭第一个选项标签。
另外,您可能希望选项值是数据库中的 id 列。
请使用表中正确的列名称。
但它应该是这样的:

<select name = "select_history" id="rec_mode">
<option selected="true" disabled="disabled"> -- SELECT -- </option>  <!-- close it here -->
<?php
    require_once 'config.php';

    $hist = mysqli_query($mysqli,"SELECT m.movieID, m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY movieID DESC"); // make sure query is correct, test it in your mysql client
    while ($row = $hist->fetch_assoc())
    {
        echo "<option value='{$row['movieID']}'>{$row['name']}</option>";
    }
?>

First, you have to close that first option tag properly.
Also, you might want the option value would be the id column from the db.
Please use the correct column names from your tables.
But it should be something like this:

<select name = "select_history" id="rec_mode">
<option selected="true" disabled="disabled"> -- SELECT -- </option>  <!-- close it here -->
<?php
    require_once 'config.php';

    $hist = mysqli_query($mysqli,"SELECT m.movieID, m.name FROM movie_names m INNER JOIN host_table ht WHERE m.movieID = ht.random_num ORDER BY movieID DESC"); // make sure query is correct, test it in your mysql client
    while ($row = $hist->fetch_assoc())
    {
        echo "<option value='{$row['movieID']}'>{$row['name']}</option>";
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文