By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,702 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,702 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Mar 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Flic wrote:
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.
js*******@attglobal.net
==================
Mar 22 '07 #2

P: n/a
Flic wrote:
$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!
Mar 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.