使用选项 PHP/SQL 填充表单


Populating Form with Options PHP/SQL

我正在做一个非常简单的html/php项目,似乎已经卡住了。我需要使用根据 sql 查询具有值的选项填充下拉表单。到目前为止,这就是我在 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>

我可以很好地加载页面,因此 html 可以正常工作,但下拉菜单为空。php在某种程度上失败了。我需要每个选项的值属性等于 sql 查询中的值。如何动态执行此操作?

任何帮助非常感谢!谢谢!

更改

$result = mysql_query($result);

自:

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

您使用了错误的变量来获取查询,并且没有检查错误(这会让您知道那里有问题)。