error in SELECT statement using vars from $_GET array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashraf02
    New Member
    • Feb 2008
    • 53

    error in SELECT statement using vars from $_GET array

    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","roo t","")
    or die (mysql_error()) ;
    mysql_select_db ("Shop_Test",$c onn) 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_Categorie s AS c on c.id =SI.Cat_ID WHERE SI.Store_ID = $_GET[Item_ID]";

    $get_item_resul ts = mysql_query ($get_item) or die (mysql_error()) ;

    if (mysql_num_rows ($get_item_resu lts)<1) {
    $display_block .= "<p><em>Inv alid item selection.</em></p>";
    } else {
    $cat_id = mysql_result ($get_item_resu lts,0,'Cat_ID') ;
    $cat_title = strtoupper (stripslashes (
    mysql_result ($get_item_resu lts,0, 'cat_title')));
    $item_title = stripslashes (mysql_result ($get_item_resu lts,0,'Item_tit le'));
    $item_price = mysql_result ($get_item_resu lts,0,'Item_Pri ce');
    $item_desc = stripslashes(my sql_result ($get_item_resu lts,0, 'item_Desc'));
    $item_image = mysql_result ($get_item_resu lts,0,'item_Ima ge');

    $display_block .= "<p><strong><em > Youre Viewing:</em><br><a href=\"catalogu e.php?Cat_ID=$C at_ID\">$cat_ti tle</a>
    &gt; $item_title </strong></p>

    <table cellpadding=3 cellspacing=3>
    <tr>
    <td valign=middle align=center><i mg src=\"$item_ima ge\"><td>
    <td valign=middle>< p><strong>Descr iption:</strong><br>$ite m_desc</p>
    <p><strong>Pric e:</strong> \$$item_price</p>";

    $get_colours = "SELECT Item_Colour FROM Store_Item_Colo ur WHERE
    Store_ID = $item_id ORDER BY Item_Colour";
    $get_colour_res ults = mysql_result ($get_colours) or die (mysql_error()) ;

    if (mysql_num_rows ($get_colour_re sults)>0) {
    $display_block .= "<p><strong>Ava ilable Colours:</strong><br>";

    while ($colours = mysql_fetch_arr ay($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_resul ts = mysql_query($ge t_sizes) or die (mysql_error()) ;

    if (mysql_num_rows ($get_size_resu lts)>0) {
    $display_block .= "<p><strong>Ava ilable Sizes:</strong></br>";

    while ($sizes = mysql_fetch_arr ay ($get_size_resu lts)) {
    $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
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    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_Categorie s 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_Categorie s 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

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by nathj
      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

      Comment

      • ashraf02
        New Member
        • Feb 2008
        • 53

        #4
        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.

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          The following gets rid of the MySQL error in line 3:
          Code:
          $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']}'";
          Ronald

          Comment

          • ashraf02
            New Member
            • Feb 2008
            • 53

            #6
            Thanks once again ronald. uve been a great help

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              You are welcome, until next time.

              Ronald

              Comment

              Working...