473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 = $_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
2 3164
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
1
by: Maciej Paras | last post by:
Hello! I've written 2 pages: one i HTML format, and second - in ASP. When I'm posting data from HTML page, I receive this error generated by ASP page: Microsoft JET Database Engine (0x80040E14)...
0
by: Morten Gulbrandsen | last post by:
C:\mysql\bin>mysql -u elmasri -pnavathe company Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 to server version: 4.1.0-alpha-max-debug Type...
0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
3
by: Matias Silva | last post by:
Hi Everyone, I wrote a for loop to build several select statements that are combined with a UNION. When I execute one of the queries separately, it works, but when I execute the query with a UNION...
7
by: Kevin | last post by:
Hi, With this, i get the error: " Microsoft VBScript runtime error '800a01ca' Variable uses an Automation type not supported in VBScript " at line 79 <% totintot=request.cookies("totintot")...
3
by: Jacob Lyles | last post by:
Howdy, I'm a but of a newbie and I'd appreciate some help with a MySQL issue I'm having. I'm trying to insert some data into MySQL from a POST form but the query breaks whenever a user fails to...
9
by: =?ISO-8859-1?B?UOlw6g==?= | last post by:
I have a page that inserts these values in the table... $fundo_nome = $_POST; $fundo_link = $_POST; $fundo_activo = $_POST; $sql = sprintf("INSERT INTO relatorio_fundo(fundo_nome,...
4
anfetienne
by: anfetienne | last post by:
Hi, I have mysql 5.045 installed on my server and i've written a code to insert values into the database. it keeps coming up with this error Error in query:You have an error in your SQL syntax;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.