Hi! I'm new to PHP, so could someone please help me? I'm having a bit of a problem... well... I don't know what to do. I'm trying to basically get something from a database. I have this whole database of "orders" that people submitted. The table is named order_item. It is laid out like this: there's "order_number" that is the shopper's session number. It's auto-incrementing. There's item_number; that is the number item that they got. It's ordinal, as in "1" really means their first item, "2" means their second item, and so on. There's catalog_number; that's the number that refers to a specific item on the catalog. There's quantity, which is how many of that specific item they ordered, and there's price, which isn't really relevant here. So, the table looks like this: order_number item_number catalog_number quantity price 1 1 4 2.00 1.00 1 2 9 5.00 0.25 1 3 5 5.00 0.25 1 4 7 1.00 5.25 2 1 2 5.00 4.31 Code (markup): I want my PHP script to basically be able to: 1. Pick only the order number that is the current user's. This is stored in $_SESSION['order_number']. 2. Echo each item number, the catalog number, and the quantity. So that would look like this, for the first person who ordered (their $_SESSION['order_number'] would be "1"): You ordered: 1. cat no: 4 qty: 2.00 2. cat no: 9 qty: 5.00 3. cat no: 5 qty: 5.00 4. cat no: 7 qty: 1.00 Code (markup): That would be AWESOME if somebody could help me do this or something like it!!!! Thanks in advance, --Joe
You can try something like this: <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } $query = 'SELECT * FROM order_item WHERE order_number = "' . mysql_real_escape_string($_SESSION['order_number']) . '"'; $result = mysql_query($query, $link); if (mysql_num_rows($result)) { echo "You ordered:<br /><br />" while ($row = mysql_fetch_assoc($result)) { echo $row['item_number'] . " cat no: " . $row['catalog_number'] . " qty: " . $row['quantity'] . "<br />"; } } ?> PHP: HTH, cheers!
Ah, thank you! I didn't know how to just assign the result to a string and get rows and... well everything. Thank you so much!!! One thing I don't understand... where did you get the $row from?
PLEEEEEEASE HEEEEELLLPPP!!! Here's my entire email program: <?php /* File: email.inc * Desc: Emails the shopper and orders@jogrammah.com. */ include_once("Vars.inc"); $link = mysql_connect($host,$user,$passwd); if (!$link) { die('Could not connect to database. Error:' . mysql_error()); } $query = 'SELECT * FROM order_item WHERE order_number = "' . mysql_real_escape_string($_SESSION['order_number']) . '"'; $result = mysql_query($query, $link); if (mysql_num_rows($result)) { $itemsmessage = "You ordered:\n\n"; while ($row = mysql_fetch_assoc($result)) { $itemsmessage = $itemsmessage . $row['item_number'] . " cat no: " . $row['catalog_number'] . " qty: " . $row['quantity'] . "\n"; } } $emailto = $_SESSION['email']; $subject = "Your order number ".$_SESSION['order_number']." -- JoGrammah.com"; $from = "From: orders@jogrammah.com"; $from2 = "From: ".$emailto; $ship_name = $_SESSION['ship_name']; $ship_street = $_SESSION['ship_street']; $ship_city = $_SESSION['ship_city']; $state = $_SESSION['ship_state']; $ship_zip = $_SESSION['ship_zip']; $phone = $_SESSION['phone']; $cc_number = $_SESSION['cc_number']; $ordertotal = $_SESSION['order_total']; $noitemsordered = $_SESSION['n_items']; $subject2 = "Order # ".$_SESSION['order_number'].": ".$_SESSION['ship_name']; $message = "Thank you for ordering at sJoGrammah.com!\n You ordered $noitemsordered item(s). Your order costs "."$"."$ordertotal. $itemsmessage \nYour order will be shipped to:\n\n$ship_name\n$ship_street\n$ship_city, $state\n$ship_zip\n\nSince our store is not fully completed, you may be contacted at $phone for further details concerning your order.\n\nSincerely,\n-JoGrammah.com staff"; $message2 = "ORDER NUMBER $_SESSION['order_number']\n\nShip to:\n$ship_name\n$ship_street\n$ship_city, $ship_state\n\nPhone number: $phone\nEmail: $emailto"; mail($emailto, $subject, $message, $from); mail("orders@jogrammah.com", $subject2, $message2, $from2); ?> PHP: I'm getting this very, very odd error. Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /hsphere/local/home/joestein/jogrammah.com/shoppingcart/Procedural/email.inc on line 45 Code (markup): There's no whitespace there!!!! It's just my variable $message2!! AAAH! PLEASE HELP!!!
I think the $_SESSION['order_number'] variable embedded in the string is causing a problem. Try this instead: $orderID = $_SESSION['order_number']; $message2 = "ORDER NUMBER $orderID\n\nShip to:\n$ship_name\n$ship_street\n$ship_city, $ship_state\n\nPhone number: $phone\nEmail: $emailto"; Code (markup):
THANK YOU SO MUCH!!!!! I did a little touching up with what you gave me and it works great!!! Now....... I want to make it say, instead of what catalog number the customer ordered, I want it to be able to say what item they ordered. Here's my email php, and I'll give you the layout of my MySQL db after that: <?php /* File: email.inc * Desc: Emails the shopper and orders@jogrammah.com. */ $connect = connect_to_db("Vars.inc"); $query = 'SELECT * FROM order_item WHERE order_number = "' . mysql_real_escape_string($_SESSION['order_number']) . '"'; $result = mysql_query($query) or die("sql_getnew: ".mysql_error()); $numrows = mysql_num_rows($result); if ($numrows) { $itemsmessage = "You ordered:\n\n"; while ($row = mysql_fetch_assoc($result)) { $itemsmessage = $itemsmessage . "Item number " . $row['item_number'] . ":" . " catalog no.: " . $row['catalog_number'] . " qty: " . $row['quantity'] . "\n"; } }; $emailto = $_SESSION['email']; $orderID = $_SESSION['order_number']; $subject = "Your order number ".$_SESSION['order_number']." -- JoGrammah.com"; $from = "From: orders@jogrammah.com"; $from2 = "From: ".$emailto; $ship_name = $_SESSION['ship_name']; $ship_street = $_SESSION['ship_street']; $ship_city = $_SESSION['ship_city']; $state = $_SESSION['ship_state']; $ship_zip = $_SESSION['ship_zip']; $phone = $_SESSION['phone']; $cc_number = $_SESSION['cc_number']; $ordertotal = $_SESSION['order_total']; $noitemsordered = $_SESSION['n_items']; $subject2 = "Order # ".$_SESSION['order_number'].": ".$_SESSION['ship_name']; $message = "Thank you for ordering at sJoGrammah.com!\n You ordered $noitemsordered item(s). Your order costs "."$"."$ordertotal. $itemsmessage \nYour order will be shipped to:\n\n$ship_name\n$ship_street\n$ship_city, $state\n$ship_zip\n\nSince our store is not fully completed, you may be contacted at $phone for further details concerning your order.\n\nSincerely,\n-JoGrammah.com staff"; $message2 = "ORDER NUMBER $orderID \n\nShip to:\n$ship_name\n$ship_street\n$ship_city, $ship_state\n\nPhone number: $phone\nEmail: $emailto"; mail($emailto, $subject, $message, $from); mail("orders@jogrammah.com", $subject2, $message2, $from2); ?> PHP: Here's the layout of my MySQL db: joe_onlineorders **database Customer_Order **table of no relevance Food **I must have been hungry... catalog of items catalog_number **EXTREMELY IMPORTANT!!!!! auto-increment name **The name of the item **the rest of the table doesn't matter really order_item **PLEASE REFER TO MY EARLIER POST ON THIS ONE Code (markup): So, that would be incredibly awesome if someone could help me: 1) Get each catalog number from each item ordered by the current user 2) Find out which catalog number corresponds to which item. tia, --Joe