Forum Moderators: coopster

Message Too Old, No Replies

I have an error in your SQL syntax;

Incorrect syntax at line 1

         

derek mcgilvray

7:48 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



Hi folks,
(I'm trying to create a shopping cart) I'm learning php and was doing alright but have been banging my head on this problem for a while now - can anyone help me?

My error message is:
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.

My script is:
<?php
session_start();
$username="web14-username";
$password="password";
$database="web14-username";

//connect to database
$conn = mysql_connect("localhost", $username, $password)
or die(mysql_error());
mysql_select_db($database,$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_title'));
$item_price = mysql_result($get_item_res,0,'item_price');
$item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc'));
$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>

scriptmasterdel

7:59 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



All i can see from the above is that you haven't declared a $item_id that's why you are getting the error.

$get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";

You have a $cat_id, so maybe this is a mistake, maybe it should be ....

$get_colors = "select item_color from store_item_color where item_id = $cat_id order by item_color";

I could be wrong ....

Del

derek mcgilvray

8:40 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



You are an absolute legend Del, thank you! I've been at it for hours and hours and have just managed to have a cup of tea while you came up with that solution - thanks again!

scriptmasterdel

3:04 pm on Jul 19, 2006 (gmt 0)

10+ Year Member



No problem!

Any other queries then PM me and i will be glad to help.

Del