首先,我可以从数据库中显示所有类别,然后我使用相同的代码来显示品牌,但是当我运行页面时,它会打印出来,如图所示:
我的表格如下:
c
出于某种原因,我只能获得品牌而不是类别,而且我看不到其他领域。
我希望你能提供帮助。
提前致谢
答案 0 :(得分:2)
您在运行此行时正在销毁连接
$conn = null;
完成brands
选择后,开始categories
选择之前。你不需要破坏连接,而是让PHP在你的脚本退出时自动执行。
另外,由于您的查询中没有任何参数,因此您不需要bindParam
次调用
<table>
<tbody>
<tr>
<td>Product Name:</td>
<td><input type="text" name="product_name"></td>
</tr>
<tr>
<td>Product Brand</td>
<td>
<select name="" id="">
<optgroup label="Select a Brand"></optgroup>
<?php
global $conn;
try{
//prepare statement
$statement=$conn->prepare('select * from brands');
//execute statement
$statement->execute();
while($result=$statement->fetch()){
$brand_id = $result['brand_id'];
$brand_name = $result['brand_name'];
echo "<option value=".$brand_id.">" .$brand_name. "</option>";
}
}
catch(PDOException $e){
echo 'Error: ' . $e->getMessage();
}
// delete the collowing line
//$conn = null;
?>
</select>
</td>
</tr>
<tr>
<td>Product Category </td>
<td>
<select name="" id="">
<optgroup label="Select a Category"></optgroup>
<?php
global $conn;
try{
//prepared statement
$statement = $conn->prepare('select * from categories');
//execute
$statement->execute();
//set the result
while($result=$statement->fetch()){
$category_id = $result['category_id'];
$category_name = $result['category_name'];
echo "<option value=".$category_id.">" .$category_name. "</option>";
}
}
catch(PDOException $e){
echo 'Error: ' . $e->getMessage();
}
// delete the collowing line
//$conn = null;
?>
</select>
</td>
</tr>
<tr>
<td>Product Image</td>
<td><input type="file" name="product_image"></td>
</tr>
<tr>
<td>Product Description</td>
<td><textarea name="product_description" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td>Product Price</td>
<td><input type="text" name="product_price"></td>
</tr>
<tr id="submit_row" align="center">
<td class="submit_row" align="center">
<input type="submit" name="submit-button">
</td>
</tr>
</tbody>
</table>