我有一个图书详细信息表格,在那里我们可以插入书的详细信息。
这种形式中的一个字段是"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>发布于 2015-11-24 08:55:53
你应该这样做:
<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。
发布于 2015-11-24 08:09:21
我可以建议您通过面向对象的approach.This连接来建立连接,因为PHP5.5.0deprecated.1您的查询似乎是正确的。您可以使用mysql_fetch_array()解析SQL结果,这将返回一个数值和关联的array2。2如果您希望只使用关联属性名来解析数据库结果,则应该使用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>参考文献:
https://stackoverflow.com/questions/33887617
复制相似问题