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??

Alfred Kimotho
  • 99
  • 3
  • 17
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman Apr 10 '17 at 23:12
  • Please update with the code for dbcon.php. Remember to remove login details. – janlindso Apr 10 '17 at 23:16

2 Answers2

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>
Alfred Kimotho
  • 99
  • 3
  • 17
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>
janlindso
  • 1,225
  • 3
  • 16
  • 41