Quote:
Hi,
>
I have a basic db that I access with MySQL query browser. Everything
seems fine to me but I am using this db as part of a php shopping
basket and when I try to add an item I get:
>
Notice: Query failed: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '>function.extract]: First argument should be an
array in functions.inc.php on line 31
Notice: Undefined variable: price in functions.inc.php on line 36
Notice: Undefined variable: price in functions.inc.php on line 39
Notice: Undefined variable: total in unctions.inc.php on line 39
>
I'm assuming the last three are caused by this problem as price should
be passed to the cart, and total is worked out using it. However
although I know mySQL code it was the MySQL query browser that
actually generated the code and I cannot see a way to view or debug
the code.
>
The db has one table in it which is made up of id, name, subname,
desc, and price.
>
The code in the php file that is being referred to is:
>
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.'
in your shopping cart</a></p>';
}
}
>
function showCart() {
global $db;
$cart = $_SESSION['cart'];
$output[] = "<form action='cart.php?action=update' method='post'
id='cart'>";
$output[] = "<table><tr><td>ID</td>";
$output[] = "<td>Name</td>";
$output[] = "<td>Price</td>";
$output[] = "<td>Quan</td>";
$output[] = "<td>Subtotal</td></tr>";
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {$contents[$item] =
(isset($contents[$item])) ? $contents[$item] + 1 : 1;}
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM acc WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = "<tr><td>".$item['id']."</td>";
$output[] = "<td>".$item['info']."</td>";
$output[] = "<td>".number_format($item['price'],2)."</td>";
$output[] = "<td>".$item['qty']."</td>";
$output[] = "<td>£".($price * $qty)."</td>";
$output[] = "<td><form method=post><input
type='hidden' name='id' value='".$item['id']."'/>";
$output[] = "<input type='submit' name='remove' value='X'/></form></
td></tr>";
$total += $price * $qty;
}
$output[] = "<tr><td colspan=4>Sub total:</td><td>£".$total."</td></
tr>";
$output[] = "<tr><td colspan=4>VAT:</td><td>£".($total * 0.175)."</
td></tr>";
$output[] = "<tr><td colspan=4>Grand total:</td><td>£".($total *
1.175)."</td></tr>";
$output[] = "<div><button type='submit'>Update cart</button></div>";
$output[] = "</table></form>";
} else {
$output[] = "<tr><td colspan=5>- No items found in cart -</td></tr></
table>";
}
return join('',$output);
}
?>
>
So I can't see any problems that would be causing this other than the
db problem but can't find out what that is exactly!
>
Any help appreciated
Thanks, Flic
>
the query. And even if it did succeed, did it return any rows? You're
assuming it id.
Neither are valid assumption.
JDS Computer Training Corp.