我想制作一个自动表格显示数据库中的所有产品,您可以在其中单击其名称并查看产品的全部详细信息。我的想法是它不会将您带到另一个 .php 文件,而是您仍会在同一页面上,这样您就不会浪费时间为每个产品创建多个 .php 文件。问题是,每当我点击一个产品时,它都不会加载产品的信息(唯一的细节是顺便说一句的描述)。
错误
致命错误:不能在第 56 行的 C:\wamp\www\goldenrod\index.php 中使用类型为 mysqli_result 的对象作为数组
这是数据库连接:
<?php
//Database Connections
$dbc = mysqli_connect('localhost', 'root', '', 'lionsierra_db') or die("Cannot find specified server");
//Product Database
$db_products = "SELECT * FROM `products`";
$products = mysqli_query($dbc, $db_products);
?>
这是功能:
<?php
//View product
if(isset($_GET['view_product'])) {
while($product=mysqli_fetch_assoc($products)) {
$products['ID'] = $_GET['view_product'];
//Display a product
echo "<p><span>
<span style='font-weight:bold;'>" . $products['ID']['name'] . "</span><br/>
<span>$" . $products['ID']['price'] . "</span><br/>
<span>" . $products['ID']['category'] . "</span><br/>
<span>" . $products['ID']['description'] . "</span>
</p>";
}
}
//View all products
echo '<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Price</th>
<th>Category</th>
</tr>';
while($product=mysqli_fetch_assoc($products)) {
echo "<tr>
<td><a href='./index.php?view_product=" . $product['ID'] . "'>". $product['name'] . "</a></td>
<td>" . $product['price'] . "</td>
<td>" . $product['category'] . "</td>
</tr>";
$ID = $product['ID'];
}
?>
我对 PHP 和 SQL 还很陌生,如果你们能帮助我,我将不胜感激。