I have a view cart script that I am using. I have the view cart script which is in an include file as function viewCart. I have the query for the cart set up in a case in a display function. I am having problems with the sybtax in my query.
This is the error I am getting in the browser which is generated by function showerror();
Turquoise Pueblo is temporarily unavailable
The following has been reported to the administrator:
Turquoise Pueblo system error: MySQL error: 1064 : You have an error in your SQL syntax near 'WHERE product_id IN (428fgsr) ORDER BY product_id ASC' at line 2 (# 256).
Please report the following to the administrator:
Error in line 12 of file /home/httpd/vhosts/turquoisepueblo.com/httpdocs/include/error.inc.
I think I know what the problem is. The product ID's in my database are varchar which means when I create a concatenated list of the product ID each product ID needs to be in quotes. I have tried adding quotes to the code I have and it doesn't work. Can someone help?
This is the code in the case statement in my display function.
// display the cart
$query = 'SELECT product_id, product_name, product_cost, product_sale
WHERE product_id IN (';
foreach($_SESSION['cart'] as $variable => $value)
$query .= $variable . ',';
$query .= substr($query, 0, -1) . ') ORDER BY product_id ASC';
if (!($result = @ mysql_query ($query, $connection)))
showerror();
$total = 0;
while ($row = @ mysql_fetch_array($result, MYSQL_ASSOC))
{
if($row["product_sale"] > 0)
{
$lineTotal = $_SESSION['cart'][$row['product_id']] * $row['product_sale'];
$total += $lineTotal;
$xtpl->assign("unitPrice", $row['product_sale']);
}
else
{
$lineTotal = $_SESSION['cart'][$row['product_id']] * $row['product_cost'];
$total += $lineTotal;
$xtpl->assign("unitPrice", $row['product_cost']);
}
$xtpl->assign("lineTotal", $lineTotal);
$xtpl->assign("quantityValue", $_SESSION['cart'][$row['product_id']]);
$xtpl->assign("quantityName", qty($row['product_id']));
$xtpl->assign("prodName", $row['product_name']);
$xtpl->assign("prodID", $row['product_id']);
$xtpl->parse("main.form.cart.row");
}
$xtpl->assign("total", $total);
$xtpl->parse("main.form.cart");
$xtpl->parse("main.form");
🙂