duanliao5995 2016-10-06 05: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 05: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部