下拉列表不显示任何值


Dropdown list displays no value

我有一个书籍详细信息表单,我们可以在其中插入书籍详细信息.
此表单中的字段之一是" Category_Id "。此字段的值来自 database.
所以我想显示存储在表名">book_catagory".
中的所有类别 ID 我尝试编写我之前也做过的代码,但这里没有在表单中显示任何值。请帮助我找到我的错误。我的代码片段如下:

<td>Enter Category ID: </td>
<td>
    <?php
        $select_query = "select Category_Id from book_catagory";
        $select_query_run = mysql_query($select_query);
        echo "<select name='category_id' id='category_id'>";
        while ($select_query_array = mysql_fetch_array($select_query_run)) {
            echo "<option value='" . htmlspecialchars($select_query_array['Category_Id']) . "' >" .
 htmlspecialchars($select_query_array["Category_Id"]) . " </option>";
        }
        echo "</select>";
    ?>
</td>

你应该这样尝试:

    <td>Enter Category ID: </td>
    <td>
        <?php
            $select_query = "select Category_Id from book_catagory";
            $select_query_run = mysql_query($select_query);
            echo "<select name='category_id' id='category_id'>";
            while ($select_query_array=mysql_fetch_array($select_query_run)) {
     ?>
<option value=' <?php echo htmlspecialchars($select_query_array['Category_Id']) ?>' >
    <?php echo htmlspecialchars($select_query_array['Category_Id']) ?></option>
            }
    <?php
            echo "</select>";
        ?>
    </td>

另外,我会说你应该使用mysqli。如果需要关联数组,也可以使用 mysql_fetch_asssoc。

我可以建议你通过面向对象的方法建立连接。这种连接到数据库服务器的方式是从 PHP 5.5.0 不推荐使用以来的。[1] 您的查询似乎是正确的。您可以使用 mysql_fetch_array() 解析 SQL 结果,这将返回一个数字和关联的数组。[2] 如果您希望仅通过关联的属性名称来解析数据库结果,则应使用 PHP 方法mysql_fetch_assoc() [3]。

您可以通过以下方式更改代码以解决问题:

<td>Enter Category ID: </td>
<td>
    <?php
        $select_query = "select Category_Id from book_catagory";
        $select_query_run = mysql_query($select_query);
        echo "<select name='category_id' id='category_id'>";
        while ($select_query_array = mysql_fetch_array($select_query_run), MYSQL_NUM) {
            echo "<option value='".htmlspecialchars($select_query_array[0])."'>" .htmlspecialchars($select_query_array[0]) . " </option>";
        }
        echo "</select>";
    ?>
</td>



引用:
[1] http://php.net/manual/de/mysqli.summary.php
[2] http://php.net/manual/de/function.mysql-fetch-array.php
[3] http://php.net/manual/de/function.mysql-fetch-assoc.php