Update session variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Update session variable?

Post 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 :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Update session variable?

Post by AbraCadaver »

I don't know what you mean by // not working. Where have you echoed this or tried to use it?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Update session variable?

Post 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
synical21
Forum Contributor
Posts: 150
Joined: Tue Jul 28, 2009 8:44 am
Location: London UK

Re: Update session variable?

Post 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
Post Reply