-1

Trying to populate a dropdown list from my database - connection is ok and in my mind the code I have should work, but currently getting a blank dropdown... Have looked at PHP- Fetch from database and store in drop down menu html as well as other tutorials but alas no luck so far!

code is as follows...

<?php
//get constants for database
require("database.php");

// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){  
    die('Not connected : ' . mysqli_error());
}

// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
    die ('Can\'t use db : ' . mysqli_error($connection));
}

$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);


echo '<select name="list" style="width:400px;">';

while($r = mysqli_fetch_assoc($result)){
    echo "<option value=".$r['alt']."</option>"; 
}

echo '</select>';
?>
Community
  • 1
  • 1
fst104
  • 816
  • 1
  • 10
  • 22

3 Answers3

3

<option> tag is broken.

Corrected code:

while($r = mysqli_fetch_assoc($result)){
  echo '<option value="'.$r['alt'].'">'.$r['alt'].'</option>'; 
}

Note: You can use single and double quotes either, but, they should be properly closed.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

Please have a look on this Select Dropdown Syntax

Syntax For Select Dropdown

<select>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
</select>

You have missed a closing of value in option.

<?php
//get constants for database
require("database.php");

// Opens a connection to a MySQL server
$connection = mysqli_connect ($server, $username, $password);
if (!$connection){  
die('Not connected : ' . mysqli_error());
}

// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
    die ('Can\'t use db : ' . mysqli_error($connection));
}

$query = "SELECT * FROM route WHERE 1";
$result = mysqli_query($connection, $query);

//Changes From Here

?>
<select name="list" style="width:400px;">
    <?
    while($r = mysqli_fetch_assoc($result))
    {?>
        <option value="<?echo $r['alt'];?>"><?echo $r['alt'];?></option>
    <?}?>
</select>
Pang
  • 9,564
  • 146
  • 81
  • 122
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
-1
<?php
        //get constants for database
        require("database.php");

        // Opens a connection to a MySQL server
        $connection = mysqli_connect ($server, $username, $password);
        if (!$connection){  
            die('Not connected : ' . mysqli_error());
            }

        // Set the active MySQL database
        $db_selected = mysqli_select_db($connection, $database);
        if (!$db_selected) {
            die ('Can\'t use db : ' . mysqli_error($connection));
        }

        $query = "SELECT * FROM route WHERE 1";
        $result = mysqli_query($connection, $query);


            echo '<select name="list" style="width:400px;">';

                while($r = mysqli_fetch_assoc($result)){
                    echo "<option value=".$r['alt'].">".$r['alt']."</option>"; 
                }

            echo '</select>';
            ?>

so if your select condition is right/hidden by you then you will have to add a display value between option tag. As per the code you have assigned values in option value tag but not given the values to be displayed. Try the above

lakshman
  • 656
  • 4
  • 18