From 5c0927af391e25745a918db8ab00c6069e761404 Mon Sep 17 00:00:00 2001 From: Myron Blair Date: Sun, 14 Jun 2026 19:56:47 +0000 Subject: [PATCH] Fix ambiguous column error on shop page JOIN query Prefixed is_active, category, product_type_id, name, description, and ORDER BY columns with table alias p to resolve ambiguity with the product_types JOIN. Co-Authored-By: Claude Sonnet 4.6 --- shop.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/shop.php b/shop.php index 31fa763..553ce1f 100644 --- a/shop.php +++ b/shop.php @@ -33,13 +33,19 @@ if ($search) { } $whereClause = implode(' AND ', $where); +// Prefix columns that are ambiguous in the JOIN query +$joinWhereClause = str_replace( + ['is_active = 1', 'category =', 'product_type_id =', '(name LIKE', 'description LIKE'], + ['p.is_active = 1', 'p.category =', 'p.product_type_id =', '(p.name LIKE', 'p.description LIKE'], + $whereClause +); // Sort $orderBy = match($sort) { - 'price_low' => 'COALESCE(sale_price, price) ASC', - 'price_high' => 'COALESCE(sale_price, price) DESC', - 'name' => 'name ASC', - default => 'created_at DESC' + 'price_low' => 'COALESCE(p.sale_price, p.price) ASC', + 'price_high' => 'COALESCE(p.sale_price, p.price) DESC', + 'name' => 'p.name ASC', + default => 'p.created_at DESC' }; // Get total count @@ -48,7 +54,7 @@ $pagination = paginate($totalProducts, $page, 12); // Get products $products = db()->fetchAll( - "SELECT p.*, pt.name AS type_name, pt.type_id AS type_slug FROM products p LEFT JOIN product_types pt ON p.product_type_id = pt.type_id WHERE {$whereClause} ORDER BY {$orderBy} LIMIT :limit OFFSET :offset", + "SELECT p.*, pt.name AS type_name, pt.type_id AS type_slug FROM products p LEFT JOIN product_types pt ON p.product_type_id = pt.type_id WHERE {$joinWhereClause} ORDER BY {$orderBy} LIMIT :limit OFFSET :offset", array_merge($params, ['limit' => $pagination['per_page'], 'offset' => $pagination['offset']]) );