Aaranihlus Aaranihlus - 1 year ago 79
MySQL Question

$_GET to create product page

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

//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);
$currentProductInfo = $currentProduct->fetch(PDO::FETCH_ASSOC);
//if isset for button
<!doctype html>
<meta charset="utf-8">
<title><?php $currentProduct['product_name']?></title>
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>';

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.

Answer Source

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.