Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with resource id problem

    <form action="order_entry.php" method="post" name="order_entry" id="order_entry" >
    Please choose the order number<br />
    <select name="order_list" id="order_list"><br/>
    </p>

    <?php
    $query_order_id = 'SELECT order_id FROM `orders` WHERE order_status > 0 ';
    $order_id_result=mysql_query($query_order_id) or die ("Unable to Make the Query:" . mysql_error() );

    while($row=mysql_fetch_array($order_id_result)){

    echo "<OPTION VALUE=".$row[order_id].">".$row[order_id]."</OPTION><br/>";
    }

    ?>

    <p>
    </select>
    <br/>Choose an item to add to the order.<br />
    <select name="orderitems" id="orderitems"><br/>
    <option value=""></option>
    </p>

    <?php
    $query = 'SELECT item_name FROM `items` ORDER BY item_name LIMIT 0, 30 ';
    $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );

    while($row=mysql_fetch_array($result)){

    echo "<OPTION VALUE=".$row[item_name].">".$row[item_name]."</OPTION>";
    }

    ?>

    </select>
    <p>
    <br />Please enter the quantity<br />
    <input name="quantity" type="text" id="quantity" size="20" maxlength="4"/>
    <input type="submit" id="submit" value="Add item to order" />
    <input type="button" id="complete" value="All items are added" />
    </p>
    <?php
    //connect to db
    include "../../scripts/basic_connect.php";
    $quantity = mysql_real_escape_string($_REQUEST['quantity']);
    $itemname = mysql_real_escape_string($_REQUEST['orderitems']);

    $orderitemsql = "select item_id from items where item_name ='".$_REQUEST["orderitems"]."'";
    $item = mysql_query($orderitemsql) or die ("Unable to Make the Query:" . mysql_error() );


    echo 'you have added'.' '."$quantity".' '.'copies of '.' '."$item";


    ?>

    </form>

    there is the code. Can anyon tell me why "$item" echoes as "Resource id#6" instead of the title of the item. I tried something like...

    $whatever = mysql_real_escape_string($_REQUEST['orderitems']);

    and then...

    $orderitemsql = "select item_id from items where item_name = '$whatever' ";

    and it got me the word not the resource id but only the first word of whatever i was getting. the worrds were all different lenghts and every instance was exactly the first word. if some one knows why either of these problems are happening it would be great.

    This is my first post and my first php project and im teaching myself as I go so be nice to the newb.

    Thanks in advance

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You need to fetch the data from the query result, see http://php.net/mysql_fetch_array
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    After you do this

    PHP Code:
    $item mysql_query($orderitemsql) or die ("Unable to Make the Query:" mysql_error() ); 
    the value of $item is the resource for the results of the query. You have to actually fetch the data to be able to do something with it (you've done it in earlier queries).

    PHP Code:
    $itemqry mysql_query($orderitemsql) or die ("Unable to Make the Query:" mysql_error() );

    $item mysql_fetch_assoc($itemqry);

    echo 
    "you have added $quantity copies of " $item['item_id']; 
    Dave .... HostMonster for all of your hosting needs

  • #4
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djm0219 View Post
    After you do this

    PHP Code:
    $item mysql_query($orderitemsql) or die ("Unable to Make the Query:" mysql_error() ); 
    the value of $item is the resource for the results of the query. You have to actually fetch the data to be able to do something with it (you've done it in earlier queries).

    PHP Code:
    $itemqry mysql_query($orderitemsql) or die ("Unable to Make the Query:" mysql_error() );

    $item mysql_fetch_assoc($itemqry);

    echo 
    "you have added $quantity copies of " $item['item_id']; 
    Ok thank you but no luck yet when i do it exactly as you suggest i get a "mysql_fetch_assoc(): supplied argument is not a valid MySQL resource " error i can get rid of the error by changing mysql_assoc to mysql_array but the variable is still empty when displayed. Any more ideas ? And thanks agan for the help

  • #5
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by irenaught View Post
    Ok thank you but no luck yet when i do it exactly as you suggest i get a "mysql_fetch_assoc(): supplied argument is not a valid MySQL resource " error i can get rid of the error by changing mysql_assoc to mysql_array but the variable is still empty when displayed. Any more ideas ? And thanks agan for the help
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>some stuff</title>
    <link href="../../main.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    <!--
    @charset "utf-8";
    .flashingRed {color: #800000; text-decoration:blink }
    .ital {font-style:italic}
    .bold {font-styleblique}
    body,td,th { color: #000033; }
    body { background-color: #FFFFFF;}

    -->
    <?php
    include "../../scripts/basic_connect.php";
    $cust_id = mysql_real_escape_string($_POST['cust_id']);
    $order_disc = mysql_real_escape_string($_REQUEST['order_disc']);
    $user_id = mysql_real_escape_string($_POST['user_id']);
    $sql = "INSERT INTO orders (cust_id, user_id, order_disc) values ('$cust_id', '$user_id', '$order_disc')";


    if (!mysql_query($sql,$link)) {
    die('Error: ' . mysql_error());
    }

    ?>
    </style>
    </head>
    <body>
    <div id="header" align="center">
    <img src="../../images/vfi_logo.jpg" alt="whatever" align="middle" longdesc="vfi_logo.jpg"/>
    </div>

    <div id="mainContent" align="center">
    <h3><span class="ital">Welcome to the V.F.I order entry screen. Please be sure you are entering items correctly!!!<br />
    If you have any problems or concerns please contact blank@blank.com<br />
    <?php
    echo "Welcome user number ".$_POST["user_id"]." please enter the order";
    ?>
    </span></h3>

    <p>
    <form action="order_entry.php" method="post" name="order_entry" id="order_entry" >
    Please choose the order number<br />
    <select name="order_list" id="order_list"><br/>
    </p>

    <?php
    $query_order_id = 'SELECT order_id FROM `orders` WHERE order_status > 0 ';
    $order_id_result=mysql_query($query_order_id) or die ("Unable to Make the Query:" . mysql_error() );

    while($row=mysql_fetch_array($order_id_result)){

    echo "<OPTION VALUE=".$row[order_id].">".$row[order_id]."</OPTION><br/>";
    }

    ?>

    <p>
    </select>
    <br/>Choose an item to add to the order.<br />
    <select name="order_items" id="order_items"><br/>
    <option value=""></option>
    </p>

    <?php
    $query = 'SELECT item_name FROM `items` ORDER BY item_name LIMIT 0, 30 ';
    $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );

    while($row=mysql_fetch_array($result)){

    echo "<OPTION VALUE=".$row[item_name].">".$row[item_name]."</OPTION>";
    }

    ?>

    </select>
    <p>
    <br />Please enter the quantity<br />
    <input name="quantity" type="text" id="quantity" size="20" maxlength="4"/>
    <input type="submit" id="submit" value="Add item to order" />
    <input type="button" id="complete" value="All items are added" />
    </p>
    <?php
    //connect to db

    include "../../scripts/basic_connect.php";
    $quantity = mysql_real_escape_string($_REQUEST['quantity']);
    $itemname = mysql_real_escape_string($_REQUEST['orderitems']);

    $orderitemsql = "select item_id from items where item_name ='".$_REQUEST["orderitems"]."'";
    $itemqry = mysql_query($orderitemsql) or die ("Unable to Make the Query:" . mysql_error() );
    $item = mysql_fetch_assoc($itemqry);

    echo 'you have added'.' '."$quantity".' '.'copies of '.' '."$item[item_id]";











    ?>

    </form>

    ok ther is the whole page. if the second to last real line of code is as it s now the output is you have added "x" copies of... and then nothing. obviously it doesnt output the periods. if i change mysql_fetch_assoc to mysql_fetch_array then the output is you have added "x" copies of Resource ID#6. please help i need it to output either the actual name of the item.

    thanks

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    When you use mysql_fetch_array the results are returned with numeric indexes not the name of the field like mysql_fetch_assoc does. If you say using mysql_fetch_array works there should be no reason mysql_fetch_assoc shouldn't work.

    In any case change the echo to:

    PHP Code:
    echo "you have added $quantity copies of " $item[0]; 
    and you should see the value being retrieved when using mysql_fetch_array.
    Dave .... HostMonster for all of your hosting needs

  • #7
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    include "../../scripts/basic_connect.php";
    $quantity = mysql_real_escape_string($_REQUEST['quantity']);
    $itemname = mysql_real_escape_string($_REQUEST['orderitems']);

    $orderitemsql = "select item_id from items where item_name ='".$_REQUEST["order_items"]."'";
    $itemqry = mysql_query($orderitemsql) or die ("Unable to Make the Query:" . mysql_error() );
    $item = mysql_fetch_assoc($itemqry);

    echo "you have added $quantity copies of " . $item[0];

    this is still returning a blank value in the output now it doesnt matter wether it is assoc or array both are just blank. Could it be something to do with the way the list for <select id="order_items> is populated from the DB

    Choose an item to add to the order.<br />
    <select name="order_items" id="order_items"><br/>
    <option value=""></option>
    </p>

    <?php
    $query = 'SELECT item_name FROM `items` ORDER BY item_name LIMIT 0, 30 ';
    $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );

    while($row=mysql_fetch_array($result)){

    echo "<OPTION VALUE=".$row[item_name].">".$row[item_name]."</OPTION>";
    }

    ?>

    </select>

    ?????

    Thanks so much for the help.

  • #8
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    p.s

    not throwing any errors. just empty output

  • #9
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Use print_r to print the array named $item and see what it contains.

    PHP Code:
    print_r($item);, 
    Dave .... HostMonster for all of your hosting needs

  • #10
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    adding the line does nothing at all should the comma at the end be a part of if?

  • #11
    New Coder
    Join Date
    Jul 2008
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    nope adding the comma breaks it. so i guess there is nothing in $item?

  • #12
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    There shouldn't have been a comma. Sorry.

    Use print statements to see what the actual query that is built for SQL looks like right before the MySQL call. Something is not as you expect.
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •