Jump to content

[SOLVED] SESSION vars not updating


jminnie

Recommended Posts

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);

?>

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

Figured it out--all by myself!  ;D

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.