order by error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    order by error

    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_r es = mysql_query($ge t_iteminfo) or die(mysql_error ());
    if (mysql_num_rows ($get_iteminfo_ res) < 1) {
    //invalid id, send away
    header("Locatio n: seestore.php");
    exit;
    } else {
    //get info
    $item_title = mysql_result($g et_iteminfo_res ,0,'item_title' );
    //add info to cart table
    $addtocart = "insert into store_shoppertr ack values ('', '$PHPSESSID', ' POST[sel_item_id]', ' POST[sel_item_qty]', ' POST[sel_item_size]', ' POST[sel_item_color]', now())";
    mysql_query($ad dtocart);
    //redirect to showcart page
    header("Locatio n: showcart.php");
    exit;
    }
    } else {
    //send them somewhere else
    header("Locatio n: 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_categorie s as c on c.id = si.cat_id where si.id = GET[item_id]";
    $get_item_res = mysql_query($ge t_item) or die (mysql_error()) ;
    if (mysql_num_rows ($get_item_res) < 1) {
    //invalid item
    $display_block .= "<P><em>Inv alid item selection.</em></p>";
    } else {
    //valid item, get info
    $cat_id = mysql_result($g et_item_res,0,' cat_id');
    $cat_title = strtoupper(stri pslashes(mysql_ result($get_ite m_res,0,'cat_ti tle')));
    $item_title = stripslashes(my sql_result($get _item_res,0,'it em_title'));
    $item_price = mysql_result($g et_item_res,0,' item_price');
    $item_desc = stripslashes(my sql_result($get _item_res,0,'it em_desc'));
    $item_image = mysql_result($g et_item_res,0,' item_image');
    //make breadcrumb trail
    $display_block .= "<P><strong><em >You are viewing:</em><br><a href=\"seestore .php?cat_id=$ca t_id\">$cat_tit le</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>
    <form method=post action=\"addtoc art.php\">";
    //get colors
    $get_colors = "select item_color from store_item_colo r where item_id = $item_id order by item_color";
    $get_colors_res = mysql_query($ge t_colors) or die(mysql_error ());
    if (mysql_num_rows ($get_colors_re s) > 0) {
    $display_block .= "<P><strong>Ava ilable Colors:</strong>
    <select name=\"sel_item _color\">";
    while ($colors = mysql_fetch_arr ay($get_colors_ res)) {
    $item_color = $colors['item_color'];
    $display_block .= "<option value=\"$item_c olor\">$item_co lor</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($ge t_sizes) or die(mysql_error ());
    if (mysql_num_rows ($get_sizes_res ) > 0) {
    $display_block .= "<P><strong>Ava ilable Sizes:</strong>
    <select name=\"sel_item _size\">";
    while ($sizes = mysql_fetch_arr ay($get_sizes_r es)) {
    $item_size = $sizes['item_size'];
    $display_block .= "
    <option value=\"$item_s ize\">$item_siz e</option>";
    }
    $display_block .= "</select>";
    }
    $display_block .= "
    <P><strong>Sele ct 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.
    Last edited by pbmods; Jun 28 '07, 06:03 PM. Reason: Fixed code tag problem.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    echo the query where you are getting error. Then examine that query for errors.

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      I added an echo on line 25.
      line 24 is the query.
      [PHP]
      $get_colors = "select item_color from store_item_colo r 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?

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        What does this line prints "echo $get_colors;"?

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          Does $item_id hold a value?

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by mwasif
            What does this line prints "echo $get_colors;"?
            I was hoping it would print this
            select item_color from store_item_colo r where item_id = $item_id order by item_color";

            I'm new to PHP.

            Comment

            • mwasif
              Recognized Expert Contributor
              • Jul 2006
              • 802

              #7
              What is the exact ouput you get? I think $item_id oes not have a value.

              Comment

              • Purple
                Recognized Expert Contributor
                • May 2007
                • 404

                #8
                Hi Nomad,

                try changing line 30 script 2 from :

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

                to

                [PHP]$get_colors = "select item_color from store_item_colo r 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

                Comment

                • nomad
                  Recognized Expert Contributor
                  • Mar 2007
                  • 664

                  #9
                  Originally posted by Purple
                  Hi Nomad,

                  try changing line 30 script 2 from :

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

                  to

                  [PHP]$get_colors = "select item_color from store_item_colo r 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\htd ocs\showitem.ph p 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.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Originally posted by nomad
                    [code=PHP]
                    <?php
                    $get_colors = "select item_color from store_item_colo r where item_id = $item_id order by item_color";.
                    [/code]
                    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.

                    Comment

                    Working...