Freestyle076
Freestyle076

Reputation: 1556

Populating Form with Options PHP/SQL

I'm working on a very simply html/php project and seem to be stuck already. I need to populate a drop down form with options that have values according to an sql query. So far this is what I have in HTML:

<html>
<title>Drop Down</title>
<body>
    <form action = "query.php" method = "POST">
        <select name = "CategoryChoice">
            <?php
                $host = "localhost";
                $user = "user";
                $pass = "pass";
                $db = "cpsc421";
                mysql_connect($host,$user,$pass);
                mysql_select_db($db);
                $stmt = "SELECT name FROM category";
                $result = mysql_query($result);
                while(list($category) = mysql_fetch_row($result)){
                    $option = '<option value="'.$category.'">'.$category.'</option>';
                    echo $option;
                    }
            ?>

        </select>
        <input type = "submit" value = "ExecuteQuery"/>
    </form>



</body>
</html>

I can load the page fine, so the html is working, but the drop down menu is empty. The php is failing in some way. I need each option's value attribute to equal values from the sql query. How can I perform this dynamically?

Any help much appreciated! Thanks!

Upvotes: 1

Views: 867

Answers (1)

Barmar
Barmar

Reputation: 780818

Change

$result = mysql_query($result);

to:

$result = mysql_query($stmt) or die(mysql_error());

You were using the wrong variable to get the query, and you weren't checking for errors (which would have let you know that you had a problem there).

Upvotes: 3

Related Questions