php - 下拉列表显示无值

标签 php html mysql

<分区>

我有一个图书详细信息表单,我们可以在其中插入图书详细信息。
此表单中的一个字段是“Category_Id”。该字段的值来自数据库。
所以我想显示存储在表名“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 - 下拉列表显示无值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887617/

相关文章:

php - 图片上传速度更快

PHP 数组到 jQuery 选项

javascript - Puppeteer:如何下载整个网页供离线使用

mysql - 如何检索连续几天发生某些事件的不同行的计数

mysql - 在派生表和日历表上应用右联接以获取两天之间的所有日期

php - 如何在mongodb中找到数组的最后一项?

php - Laravel 使用带参数的模型工厂生成对象

javascript - 如何在表格幻灯片中的点上方放置向上和向下箭头?

javascript - 页面不居中

python - 要求已经满足 : mysql-python in/usr/local/lib/python2. 7/site-packages