简体   繁体   中英

how do I fetch my data from database in select dropdown list

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>

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>

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

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>";
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM