473,396 Members | 2,061 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.

order by error

nomad
664 Expert 512MB
When I run this program I get an error.
The error appears after I select the second link. I get this error:
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 'order by item_color' at line 1

I think the error points to another php file which is listed below called showitem.php

[PHP] <?php
session_start();
//connect to database
$conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
mysql_select_db("storefront_db",$conn) or die(mysql_error());
if ( POST[sel_item_id] != "") {
//validate item and get title and price
$get_iteminfo = "select item_title from store_items where id = POST[sel_item_id]";
$get_iteminfo_res = mysql_query($get_iteminfo) or die(mysql_error());
if (mysql_num_rows($get_iteminfo_res) < 1) {
//invalid id, send away
header("Location: seestore.php");
exit;
} else {
//get info
$item_title = mysql_result($get_iteminfo_res,0,'item_title');
//add info to cart table
$addtocart = "insert into store_shoppertrack values ('', '$PHPSESSID', ' POST[sel_item_id]', ' POST[sel_item_qty]', ' POST[sel_item_size]', ' POST[sel_item_color]', now())";
mysql_query($addtocart);
//redirect to showcart page
header("Location: showcart.php");
exit;
}
} else {
//send them somewhere else
header("Location: seestore.php");
exit;
}
?>
[/PHP]

This code do not work...
showitem.php
[PHP]
<?php
session_start();
//connect to database
$conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
mysql_select_db("storefront_db",$conn) or die(mysql_error());
$display_block = "<h1>My Store - Item Detail</h1>";
//validate item
$get_item = "select c.id as cat_id, c.cat_title, si.item_title, si.item_price, si.item_desc, si.item_image from store_items as si left join store_categories as c on c.id = si.cat_id where si.id = GET[item_id]";
$get_item_res = mysql_query($get_item) or die (mysql_error());
if (mysql_num_rows($get_item_res) < 1) {
//invalid item
$display_block .= "<P><em>Invalid item selection.</em></p>";
} else {
//valid item, get info
$cat_id = mysql_result($get_item_res,0,'cat_id');
$cat_title = strtoupper(stripslashes(mysql_result($get_item_res ,0,'cat_title')));
$item_title = stripslashes(mysql_result($get_item_res,0,'item_ti tle'));
$item_price = mysql_result($get_item_res,0,'item_price');
$item_desc = stripslashes(mysql_result($get_item_res,0,'item_de sc'));
$item_image = mysql_result($get_item_res,0,'item_image');
//make breadcrumb trail
$display_block .= "<P><strong><em>You are viewing:</em><br><a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a> &gt; $item_title</strong></p>
<table cellpadding=3 cellspacing=3>
<tr>
<td valign=middle align=center><img src=\"$item_image\"></td>
<td valign=middle><P><strong>Description:</strong><br>$item_desc</p>
<P><strong>Price:</strong> \$item_price</p>
<form method=post action=\"addtocart.php\">";
//get colors
$get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";
$get_colors_res = mysql_query($get_colors) or die(mysql_error());
if (mysql_num_rows($get_colors_res) > 0) {
$display_block .= "<P><strong>Available Colors:</strong>
<select name=\"sel_item_color\">";
while ($colors = mysql_fetch_array($get_colors_res)) {
$item_color = $colors['item_color'];
$display_block .= "<option value=\"$item_color\">$item_color</option>";
}
$display_block .= "</select>";
}
//get sizes
$get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size";
$get_sizes_res = mysql_query($get_sizes) or die(mysql_error());
if (mysql_num_rows($get_sizes_res) > 0) {
$display_block .= "<P><strong>Available Sizes:</strong>
<select name=\"sel_item_size\">";
while ($sizes = mysql_fetch_array($get_sizes_res)) {
$item_size = $sizes['item_size'];
$display_block .= "
<option value=\"$item_size\">$item_size</option>";
}
$display_block .= "</select>";
}
$display_block .= "
<P><strong>Select Quantity:</strong>
<select name=\"sel_item_qty\">";
for($i=1; $i<11; $i++) {
$display_block .= "<option value=\"$i\">$i</option>";
}
$display_block .= "
</select>
<input type=\"hidden\" name=\"sel_item_id\" value=\" GET[item_id]\">
<P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p>
</form>
</td>
</tr>
</table>";
}
?>
<HTML>
<HEAD>
<TITLE>My Store</TITLE>
</HEAD>
<BODY>
<?php echo $display_block; ?>
</BODY>
</HTML>
[/PHP]

any help would be great. If you need to see my db tables please let me know
thanks
nomad

ps sorry the tag for php is missing up my php code.
Jun 28 '07 #1
9 2476
mwasif
802 Expert 512MB
echo the query where you are getting error. Then examine that query for errors.
Jun 28 '07 #2
nomad
664 Expert 512MB
I added an echo on line 25.
line 24 is the query.
[PHP]
$get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";
echo $get_colors;
[/PHP]

Same Error printed out before the echo was printed.

nomad
PS This is not a class assignment I'm learn PHP and Msql from a book called PHP MySQL and Apache all in one by Julie Meloni

PSS would I get an error if the images are in the wrong dir?
and can the images be .jpeg?
Jun 28 '07 #3
mwasif
802 Expert 512MB
What does this line prints "echo $get_colors;"?
Jun 28 '07 #4
mwasif
802 Expert 512MB
Does $item_id hold a value?
Jun 28 '07 #5
nomad
664 Expert 512MB
What does this line prints "echo $get_colors;"?
I was hoping it would print this
select item_color from store_item_color where item_id = $item_id order by item_color";

I'm new to PHP.
Jun 28 '07 #6
mwasif
802 Expert 512MB
What is the exact ouput you get? I think $item_id oes not have a value.
Jun 28 '07 #7
Purple
404 Expert 256MB
Hi Nomad,

try changing line 30 script 2 from :

[PHP]$get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";[/PHP]

to

[PHP]$get_colors = "select item_color from store_item_color where item_id = '".$item_id."' order by item_color";[/PHP]

if that doesn't help, post the range of values you are expecting for $item_id..

also follow mwasif's suggestion of echoing the sql statement on line 30 prior to execution..

Regards Purple
Jun 28 '07 #8
nomad
664 Expert 512MB
Hi Nomad,

try changing line 30 script 2 from :

[PHP]$get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";[/PHP]

to

[PHP]$get_colors = "select item_color from store_item_color where item_id = '".$item_id."' order by item_color";[/PHP]

if that doesn't help, post the range of values you are expecting for $item_id..

also follow mwasif's suggestion of echoing the sql statement on line 30 prior to execution..

Regards Purple
when I added your code I did get this error
Parse error: syntax error, unexpected T_VARIABLE in C:\Program Files\xampp\htdocs\showitem.php on line 35

OK how do I fix this please.
line 35 is the code that I added that you gave me.

nomad
PS if I was to place an echo where do I add it and how do I write it.

thanks
nomad

PS I do have values for those fields.
Jun 28 '07 #9
Atli
5,058 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";.
  4.  
I can't see where you set the $item_id variable. Which PHP would then parse into nothing and MySQL would try to parse the 'order by' clause as the value for the boolean expression, which will result into precisely the error you gave us.

Make sure the $item_id realy has a value.
Jul 15 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
18
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
7
by: JJ_377 | last post by:
Can someone tell me why SQL seems to ignore my order by clause? I tried to run through the debugger, but the debugger stops at the select statement line and then returns the result set; so, I have...
4
by: dave | last post by:
hi all, hope someone can help.... i'm having trouble calling an SP where the ORDER BY operator is specified as a parameter when the SP is called my SP is..... CREATE PROCEDURE...
1
by: Thomas Schoen | last post by:
Hi, is it possible to use a parameter of a plpgsql-function to order a selection inside the function? What i would like to do is pass a column-name/alias-name to a plpgsql function and use...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
9
by: phillip.s.powell | last post by:
Ok, you have three tables. You're supposed to be able to not only sort (ORDER BY) according to a_name, no problem, but you must also have the ability to sort (ORDER BY) the relationship between...
3
by: ylekot88 | last post by:
Hi I am a student, I am trying to figure this out. I am trying to run a select statement that is asking for customer#, date, and total price of each order. ensure results are in chronological...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.