0

am trying to populate a dropdown list from mysql database table

here is the code

<div class="form-group">
                Select Make
                        <?php
                            include '../db_config/dbcon.php';
                            $sql = "SELECT * FROM vehicle_details";
                            $result = mysql_query($sql);

                            echo "<select name='vehicle_make'>";
                            while ($row = mysql_fetch_array($result)) {
                                echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
                            }
                            echo "</select>";
                        ?>
                </div>

This is what is displayed by the code dropdown screenshot

Where am i going wrong??

2
  • WARNING: If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and has been removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way helps explain best practices. Make sure your user parameters are properly escaped or you will end up with severe SQL injection bugs. Commented Apr 10, 2017 at 23:12
  • Please update with the code for dbcon.php. Remember to remove login details. Commented Apr 10, 2017 at 23:16

2 Answers 2

1

This worked too, its a modification of janlindo's above

                 <div class="form-group">
                                    Select Make <?php
                                    include '../db_config/dbcon.php';
                                    $sql = "SELECT * FROM vehicle_details";
                                    $result = $conn->query($sql);
                                    if ($result->num_rows > 0) {
                                        echo "<select name='vehicle_make'>";
                                        // output data of each row
                                        while($row = $result->fetch_assoc()) {
                                          echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
                                        }
                                        echo "</select>";
                                    } 
                                    ?>
                                </div>
Sign up to request clarification or add additional context in comments.

Comments

0

Depending on whats in your dbcon.php, but here's an example using mysqli_query:

<div class="form-group">
    Select Make <?php
    // start of dbcon
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";


    $conn = new mysqli($servername, $username, $password, $dbname);
    //end of dbcon

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT * FROM vehicle_details";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

        echo "<select name='vehicle_make'>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
          echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>";
        }
        echo "</select>";
    } 
    $conn->close();
    ?>
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.