f



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

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 =3D $_SESSION['cart'];
	if (!$cart) {
		return '<p>You have no items in your shopping cart</p>';
	} else {
		// Parse the cart session variable
		$items =3D explode(',',$cart);
		$s =3D (count($items) > 1) ? 's':'';
		return '<p>You have <a href=3D"cart.php">'.count($items).' item'.$s.'
in your shopping cart</a></p>';
	}
}

function showCart() {
	global $db;
	$cart =3D $_SESSION['cart'];
		$output[] =3D "<form action=3D'cart.php?action=3Dupdate' method=3D'post'
id=3D'cart'>";
  		$output[] =3D "<table><tr><td>ID</td>";
		$output[] =3D "<td>Name</td>";
		$output[] =3D "<td>Price</td>";
		$output[] =3D "<td>Quan</td>";
		$output[] =3D "<td>Subtotal</td></tr>";
	if ($cart) {
		$items =3D explode(',',$cart);
		$contents =3D array();
		foreach ($items as $item) {$contents[$item] =3D
(isset($contents[$item])) ? $contents[$item] + 1 : 1;}
		foreach ($contents as $id=3D>$qty) {
			$sql =3D 'SELECT * FROM acc WHERE id =3D '.$id;
			$result =3D $db->query($sql);
			$row =3D $result->fetch();
			extract($row);
  			$output[] =3D "<tr><td>".$item['id']."</td>";
			$output[] =3D "<td>".$item['info']."</td>";
			$output[] =3D "<td>".number_format($item['price'],2)."</td>";
			$output[] =3D "<td>".$item['qty']."</td>";
                	$output[] =3D "<td>&pound;".($price * $qty)."</td>";
                	$output[] =3D "<td><form method=3Dpost><input
type=3D'hidden' name=3D'id' value=3D'".$item['id']."'/>";
			$output[] =3D "<input type=3D'submit' name=3D'remove' value=3D'X'/></for=
m></
td></tr>";
                        $total +=3D $price * $qty;
		}
		$output[] =3D "<tr><td colspan=3D4>Sub total:</td><td>=A3".$total."</td></
tr>";
		$output[] =3D "<tr><td colspan=3D4>VAT:</td><td>=A3".($total * 0.175)."</
td></tr>";
		$output[] =3D "<tr><td colspan=3D4>Grand total:</td><td>=A3".($total *
1=2E175)."</td></tr>";
		$output[] =3D "<div><button type=3D'submit'>Update cart</button></div>";
		$output[] =3D "</table></form>";
	} else {
		$output[] =3D "<tr><td colspan=3D5>- 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

0
FelicityP (10)
3/21/2007 11:25:39 PM
comp.databases.mysql 6944 articles. 1 followers. laredotornado (869) is leader. Post Follow

2 Replies
467 Views

Similar Articles

[PageSpeed] 31

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!
0
Toby
3/22/2007 3:38:42 AM
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.
jstucklex@attglobal.net
==================
0
Jerry
3/22/2007 4:24:39 AM
Reply: