Page 1 of 1

Update session variable?

Posted: Fri Dec 18, 2009 5:15 pm
by synical21
I have done a few google searches trying to find out how i can update a session value without destroying and restarting all the sessions(not what i want), from google i mainly find javascript soloutions to changing session variables.

Here is segment of code in question:

Code: Select all

 
// calculate the cost of the job
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
 
 
 try {
        if ($total_cost > $user_cost) {
         throw new Exception("incorrect funds"); // does the user have the correct amount of money?
       }
 
// if the user does have enough money process the update in DB 
else{
    $_SESSION['user_money'] = round($user_cost - $total_cost, 2); // not working
 
     
      $sql = "UPDATE `users`
    SET users.user_money = round($user_cost - $total_cost, 2)
  WHERE users.id = '$_SESSION[user_id]'";
 $result = mysql_query($sql)
     or die('Invalid query: ' . $sql . ' - Error is ' . mysql_error());
     
    
}
 
As you can see when the DB is updated i also tried to update the session value of user_money. This has had no effect so i just came here to find the correct way to update a session value. Thanks :)

Re: Update session variable?

Posted: Fri Dec 18, 2009 5:23 pm
by AbraCadaver
I don't know what you mean by // not working. Where have you echoed this or tried to use it?

Re: Update session variable?

Posted: Fri Dec 18, 2009 5:28 pm
by synical21
It is echo'd on the template of the site, so 70% of the pages will have:

User Money: $

Code: Select all

<? echo $_SESSION['user_money'] ;?>
So once this process runs and for example the user goes back to the main page where the user_money is echo'd it will not be updated. As this session is not updated it creates a flaw in the costs of the process as you see:

Code: Select all

 
 try {
        if ($total_cost > $user_cost) {
        throw new Exception("incorrect funds"); // does the user have the correct amount of money?
       }
 
the variable $user_cost = $_SESSION[user_money] so if the variable is never updated the process can run multiple times as the money is never being deducted so every user has infinate money 8O

Re: Update session variable?

Posted: Sat Dec 19, 2009 10:08 am
by synical21
Problem fixed, silly error:

Code: Select all

 
// calculate the cost of the job
$total_cost = round(($_POST['amountworkers'] * $_POST['perperson'] + $_POST['feature']) * 1.05, 2);
$user_cost = $_SESSION['user_money'];
 
 
 try {
        if ($total_cost > $user_cost) {
         throw new Exception("incorrect funds"); // does the user have the correct amount of money?
       }
 
// if the user does have enough money process the update in DB 
else{
    
    session_start(); // forgot to add session_start
    $_SESSION['user_money'] = round($user_cost - $total_cost, 2); // 'was' not working
 
     
      $sql = "UPDATE `users`
    SET users.user_money = round($user_cost - $total_cost, 2)
  WHERE users.id = '$_SESSION[user_id]'";
 $result = mysql_query($sql)
     or die('Invalid query: ' . $sql . ' - Error is ' . mysql_error());
 
 
No session_start was added in the current script, it was added in an include file that was the problem