Connecting Tech Pros Worldwide Help | Site Map

PHP saying error in mysql syntax, but written my mysql query browser!

Flic
Guest
 
Posts: n/a
#1: Mar 22 '07
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>&pound;".($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

Jerry Stuckle
Guest
 
Posts: n/a
#2: Mar 22 '07

re: PHP saying error in mysql syntax, but written my mysql query browser!


Flic wrote:
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>&pound;".($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
>
Did your query succeed? You assume it did and never check for errors on
the query. And even if it did succeed, did it return any rows? You're
assuming it id.

Neither are valid assumption.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Toby A Inkster
Guest
 
Posts: n/a
#3: Mar 22 '07

re: PHP saying error in mysql syntax, but written my mysql query browser!


Flic wrote:
Quote:
$sql = 'SELECT * FROM acc WHERE id = '.$id;
Try adding this:

print "<!--{$sql}-->\n";

It should help you to debug.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Closed Thread