PHP how to update a session from database?
500
ı want to update my session variable this code:
if (isset($_POST['topla'])) {
$kazanc_cow = $_SESSION['cow'] * "0.003";
$kazanc_chicken = $_SESSION['chicken'] * "0.001";
$db_kazanc = $_SESSION['kazanc'];
$toplam_kazanc = $db_kazanc + $kazanc_chicken + $kazanc_cow;
$uid = $_SESSION['user_id'];
$sql = "UPDATE users SET kazanc='$toplam_kazanc' WHERE id='$uid'";
if ($con->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
I am get variable from my database$_SESSION['kazanc'] = $row["kazanc"];
usingmysqli_fetch_array
method. how ı update my session kazanc variable from database?
Answer
Solution:
You should check the session variables actually exist before trying to work with them and when you are multiplying make sure you multiply numbers not a mix of numbers and strings. Also, to avoid SQL injection use
prepared statements
as a matter of course where user input is expected or possible.