jminnie Posted November 16, 2007 Share Posted November 16, 2007 I've got an order summary page that displays customer info and details of their order based on a db query. There are text fields in the display that contain the order's shipping & discount amounts and that the user can alter if they want. When user submits the form, the page reloads, having written the new info to the database. The text fields should now display the new amounts, because when the page reloaded, the session vars that populate the text fields were assigned values from the db. Sadly, this does not happen. The submit button successfully reloads the page, writes to the database, but session vars remain the same. Session vars don't update until the next form submit--they're always one step behind, but I don't understand why. Anyone able to help? Here's the gist of the code... <?php //initialize the session if (!isset($_SESSION)) { session_start(); } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $colname_getOrderProfile = "-1"; if (isset($_GET['id'])) { $colname_getOrderProfile = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']); } mysql_select_db($myDB, $myConnection); $query_getOrderProfile = sprintf("SELECT * FROM (((tbl_orderSummary JOIN tbl_orderDetails ON order_ID=details_orderID) JOIN tbl_products ON details_prodID=prod_ID) JOIN tbl_customers ON order_custID=cust_ID) LEFT JOIN tbl_shipAddress ON order_shipID=ship_ID WHERE order_ID = %s", GetSQLValueString($colname_getOrderProfile, "int")); $getOrderProfile = mysql_query($query_getOrderProfile, $myConnection) or die(mysql_error()); $row_getOrderProfile = mysql_fetch_assoc($getOrderProfile); $totalRows_getOrderProfile = mysql_num_rows($getOrderProfile); if ($getOrderProfile) { // assign values to session vars for this order $_SESSION['orderID'] = $row_getOrderProfile['details_orderID']; $_SESSION['priceListID'] = $row_getOrderProfile['cust_priceListID']; $_SESSION['orderSubTotal'] = $row_getOrderProfile['order_subTotal']; $_SESSION['custID'] = $row_getOrderProfile['cust_ID']; $_SESSION['custName'] = $row_getOrderProfile['cust_company']; $_SESSION['custEmail'] = $row_getOrderProfile['cust_email']; $_SESSION['orderPackaging'] = $row_getOrderProfile['order_packaging']; $_SESSION['orderDiscount'] = $row_getOrderProfile['order_discount']; $_SESSION['orderShipping'] = $row_getOrderProfile['order_shipping']; $_SESSION['orderShipped'] = $row_getOrderProfile['order_shipped']; $_SESSION['orderPaid'] = $row_getOrderProfile['order_paid']; $orderTotal = $_SESSION['orderSubTotal'] + $_SESSION['orderPackaging'] - $_SESSION['orderDiscount'] + $_SESSION['orderShipping']; } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "submitChanges")) { $updateSQL = sprintf("UPDATE tbl_orderSummary SET order_shipping=%s, order_discount=%s, order_packaging=%s WHERE order_ID=%s", GetSQLValueString($_POST['orderShipping'], "double"), GetSQLValueString($_POST['orderDiscount'], "double"), GetSQLValueString($_POST['orderPackaging'], "double"), GetSQLValueString($_SESSION['orderID'], "int")); mysql_select_db($myDB, $myConnection); $Result1 = mysql_query($updateSQL, $myConnection) or die(mysql_error()); } ?> <html> <head> </head> <body> <h3>Order Details: </h3> <form method="POST" action="<?php echo $editFormAction; ?>" name="submitChanges" id="submitChanges"> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <?php $i = 1; do { ?> <tr> <td> </td> <td><?php echo $row_getOrderProfile['prod_code']; ?></td> <td><?php echo $row_getOrderProfile['prod_name']; ?></td> <td><?php echo $row_getOrderProfile['details_units']; ?> units</td> <td>@</td> <td><?php echo number_format($row_getOrderProfile['details_unitPrice'],2); ?></td> <td>=</td> <td>$ <?php echo number_format(($row_getOrderProfile['details_units']*$row_getOrderProfile['details_unitPrice']),2);?></td> </tr> <?php $i += 1; } while ($row_getOrderProfile = mysql_fetch_assoc($getOrderProfile)); ?> <tr> <td colspan=6> </td> <td>Subtotal:</td> <td nowrap>$ <?php echo number_format($_SESSION['orderSubTotal'],2); ?></td> </tr> <tr> <td colspan=6> </td> <td>Packaging:</td> <td>$ <input name="orderPackaging" type="text" id="orderPackaging" value="<?php echo number_format($_SESSION['orderPackaging'],2); ?>" size="7" maxlength="7"></td> </tr> <tr> <td colspan=6> </td> <td>Discount:</td> <td><span class="red">$</span> <input name="orderDiscount" type="text" class="red" id="orderDiscount" value="<?php echo number_format($_SESSION['orderDiscount'],2); ?>" size="7" maxlength="7"></td> </tr> <tr> <td colspan=6> </td> <td>Shipping:</td> <td>$ <input name="orderShipping" type="text" id="orderShipping" value="<?php echo number_format($_SESSION['orderShipping'],2); ?>" size="7" maxlength="7"></td> </tr> <tr> <td colspan=5> </td> <td><input name="Submit" type="submit" value="UPDATE ORDER INFO"></td> <td>Total:</td> <td>$ <?php echo number_format($orderTotal,2); ?></td> </tr> </table> <input type="hidden" name="MM_update" value="submitChanges"> </form> </body> </html> <?php mysql_free_result($getOrderProfile); ?> Quote Link to comment https://forums.phpfreaks.com/topic/77557-solved-session-vars-not-updating/ Share on other sites More sharing options...
Asperon Posted November 16, 2007 Share Posted November 16, 2007 doesn't session_start() need to be be called prior to any $_SESSION calls? <?php //initialize the session if (!isset($_SESSION)) { session_start(); } should be <?php //initialzie the session session_start(); if(!isset($_SESSON)){ //do nothing } Quote Link to comment https://forums.phpfreaks.com/topic/77557-solved-session-vars-not-updating/#findComment-392574 Share on other sites More sharing options...
jminnie Posted November 16, 2007 Author Share Posted November 16, 2007 Yes, though I think the code snippet as I had it (compliments of Dreamweaver) simply checks that the session hasn't already been started. At any rate, I tried your suggestion, with no impact on my little problem. Thanks tho! Quote Link to comment https://forums.phpfreaks.com/topic/77557-solved-session-vars-not-updating/#findComment-392605 Share on other sites More sharing options...
jminnie Posted November 16, 2007 Author Share Posted November 16, 2007 Sudden insight...nothing like walking away for a bit! Session vars are stored on the server, right? So when I recall the page the first time, it sets the new value of the session var based on user input, but doesn't retrieve the new value of the session var until another call is made to the server. That's why the display is always one step behind. Any ideas on how to get around this? Are there any server functions I might use? Quote Link to comment https://forums.phpfreaks.com/topic/77557-solved-session-vars-not-updating/#findComment-392633 Share on other sites More sharing options...
jminnie Posted November 16, 2007 Author Share Posted November 16, 2007 Figured it out--all by myself! I moved the code snippet that deals with the form submit so that it occurs BEFORE the call to the database. Problem wasn't that the session vars were not updating, but that I was retrieving old data before the update had occurred. Quote Link to comment https://forums.phpfreaks.com/topic/77557-solved-session-vars-not-updating/#findComment-392668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.