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

error in SELECT statement using vars from $_GET array

P: 53
can some one please help me i am getting this error but i cant seem to figure out wat is wrong on line 3 everything looks fine

[PHP]<?php
$conn = mysql_connect("localhost","root","")
or die (mysql_error());
mysql_select_db("Shop_Test",$conn) or die (mysql_error());

$display_block = "<h1> My Store </h1>";

$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.Store_ID = $_GET[Item_ID]";

$get_item_results = mysql_query ($get_item) or die (mysql_error());

if (mysql_num_rows($get_item_results)<1) {
$display_block .= "<p><em>Invalid item selection.</em></p>";
} else {
$cat_id = mysql_result ($get_item_results,0,'Cat_ID');
$cat_title = strtoupper (stripslashes (
mysql_result ($get_item_results,0, 'cat_title')));
$item_title = stripslashes (mysql_result ($get_item_results,0,'Item_title'));
$item_price = mysql_result ($get_item_results,0,'Item_Price');
$item_desc = stripslashes(mysql_result ($get_item_results,0, 'item_Desc'));
$item_image = mysql_result ($get_item_results,0,'item_Image');

$display_block .= "<p><strong><em> Youre Viewing:</em><br><a href=\"catalogue.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>";

$get_colours = "SELECT Item_Colour FROM Store_Item_Colour WHERE
Store_ID = $item_id ORDER BY Item_Colour";
$get_colour_results = mysql_result ($get_colours) or die (mysql_error());

if (mysql_num_rows($get_colour_results)>0) {
$display_block .= "<p><strong>Available Colours:</strong><br>";

while ($colours = mysql_fetch_array($get_colour_results)) {
$item_colour = $colours['Item_Colour'];
$display_block .= "item_colour<br>";
}
}

$get_sizes = "SELECT Item_size FROM store_item_size WHERE
Store_ID = $item_id ORDER BY Item_size";
$get_size_results = mysql_query($get_sizes) or die (mysql_error());

if (mysql_num_rows ($get_size_results)>0) {
$display_block .= "<p><strong>Available Sizes:</strong></br>";

while ($sizes = mysql_fetch_array ($get_size_results)) {
$item_size = $sizes ['Item_size'];
$diplay_block .= "$item_size<br>";
}
}

$display_block .= "
</td>
</tr>
</table>";
}
?>[/PHP]

Please help i have to do a project thanks in advance
Feb 26 '08 #1
Share this Question
Share on Google+
6 Replies


nathj
Expert 100+
P: 938
Hi,

I've had a look over the code and I assume the first SQL is the one with the problem. Is that the case? If so then the following may be of help, if not then it's probably no good to you.

You have:
[php]
$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.Store_ID = $_GET[Item_ID]";
[/php]

I have never managed to make the use of the $_GET array work directly in a SQL statement like that. So I would try:
[php]
$lnItemID = $_GET['Item_ID'] ;
$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 SI LEFT OUTER JOIN
Store_Categories c on c.id = SI.Cat_ID WHERE SI.Store_ID = $lnItemID";
[/php]
The other changes are just personal preferences but whenever I use that syntax it works first time.

The other potential issue I spotted was that you are not passing a link identifier (defined at the top of your code as $conn) to the mysql_query - this needs to be the second parameter.

I hope this helps you out.
nathj
Feb 26 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
The other potential issue I spotted was that you are not passing a link identifier (defined at the top of your code as $conn) to the mysql_query - this needs to be the second parameter.
No entirely true. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. See php documentation.

Ronald
Feb 26 '08 #3

P: 53
Thanks Nathj for ur reply but i had no luck with the new code still coming up with the same error. any other suggestions everything seems fine ive been sifting through the code up and down but cant seem to find the error.

please help i need to complete this for my project.
Feb 26 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
The following gets rid of the MySQL error in line 3:
Expand|Select|Wrap|Line Numbers
  1. $get_item = "SELECT c.id as Cat_ID, c.cat_title, SI.Item_title, SI.Item_Price, SI.item_desc, SI.item_Image 
  2. FROM Store_Items AS SI 
  3. LEFT JOIN Store_Categories AS c on c.id =SI.Cat_ID 
  4. WHERE SI.Store_ID = '{$_GET['Item_ID']}'";
Ronald
Feb 26 '08 #5

P: 53
Thanks once again ronald. uve been a great help
Feb 26 '08 #6

ronverdonk
Expert 2.5K+
P: 4,258
You are welcome, until next time.

Ronald
Feb 26 '08 #7

Post your reply

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