-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>';
?>
2
  • 6
    Your option-tag is broken. Should be something like echo "<option value='".$r['alt']."'>[Text in dropdown here]</option>"; Commented Sep 29, 2015 at 12:37
  • Yep! Works fine, thanks very much! Commented Sep 29, 2015 at 12:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

3 Comments

Why should the OP try this? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
Ok @JayBlanchard. Sorry. Explaining. Editing.
Why are you including the entire code? Just include the areas you fixed.
-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

1 Comment

Why are you including the entire code? Just include the areas you fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.