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!
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.
// 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
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.
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:
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
// 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