duanliao5995 2016-10-06 13:43
浏览 12
已采纳

$ _GET创建产品页面

I'm trying to create a product page based on the passed in ID from the URL, I get this error:

Fatal error: Cannot use object of type Product as array in E:\xampp\htdocs\DiamondCommerce\product.php on line 4

<?php
//URL would be diamond.dev/product?{id}
require_once 'dbconfig.php';
$product_id = $_GET[$product['product_id']]; 
$currentProduct = $DB_con->prepare("SELECT * FROM products where product_id = :this_product_id LIMIT 1");
$product_id->bindParam(":this_product_id"   , $product_id);   
$currentProduct->execute();
$currentProductInfo = $currentProduct->fetch(PDO::FETCH_ASSOC);
//if isset for button
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php $currentProduct['product_name']?></title>
</head>
<body>
<?php 
foreach($currentProductInfo as $currentProduct) 
{
echo '<p>',$currentProduct['product_name'],'</p>';  
echo '<p>',$currentProduct['product_description'],'</p>';   
echo '<p>',$currentProduct['product_price'],'</p>'; 
echo '<p>',$currentProduct['product_image'],'</p>';
echo '<a href="#" class="btn btn-primary" name="addToBasket">Add to Basket</a>';
}
?>
</body>
</html>

The URL looks fine (example: http://diamond.dev/product.php?id=5), which is what I want, but I don't know what is causing this error. Greatly appreciate any help.

  • 写回答

1条回答 默认 最新

  • doumiao0498 2016-10-06 13:47
    关注

    The error comes from this line:

    <title><?php $currentProduct['product_name']?></title>
    

    Which is fair enough because $currentProduct is defined as

    $currentProduct = $DB_con->prepare("SELECT * FROM products where product_id = :this_product_id LIMIT 1");
    

    In other words $currentProduct is a prepared statement and not an array. You probably meant to use $currentProductInfo in your title.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的