So, im trying to subtract 2 variables, $price to $user_balance (when inserting post, i want the (new)$user_balance=(current)$user_balance - $price , but it isnt subtracting, how can it be fixed ( this code that doesnt work is above exit();)
example:
(balance is in users table)
(posts are going to the posts table)
balance before introducing post into the database : 100
product price introduced in the database: 50
After introducing the product price in the database balance should be 100-50 , so 50
function insertPost(){
if(isset($_POST['sub'])){
global $con;
global $user_id;
$content = htmlentities($_POST['content']);
$content2 = htmlentities($_POST['content2']);
$price = htmlentities($_POST['price']);
$pclass = htmlentities($_POST['pclass']);
$specificclass = htmlentities($_POST['specificclass']);
$upload_image = $_FILES['upload_image']['name'];
$image_tmp = $_FILES['upload_image']['tmp_name'];
$random_number = rand(1, 100);
$user_balance = $row_posts['user_balance'];
if(strlen($content) > 250){
echo "<script>alert('Utiliza menos de 250 carácteres)</script>";
echo "<script>window.open('home.php', '_self')</script>";
}else{
move_uploaded_file($image_tmp, "imagepost/$upload_image.$random_number");
$insert = "insert into posts (user_id, post_content, post_content2, post_price, post_class, post_specificclass, upload_image, post_date) values('$user_id', '$content', '$content2', '$price', '$pclass', '$specificclass', '$upload_image.$random_number', NOW())";
$run = mysqli_query($con, $insert);
$user_balance = $user_balance - $price;
if($run){
echo "<script>window.open('home.php', '_self')</script>";
$update = "update users set posts='yes' where user_id='$user_id'";
$update = "update users set user_balance='$user_balance - $price' where user_id='$user_id'";
$run_update = mysqli_query($con, $update);
}
exit();
}
}
}
It seems you do substraction twice
change
$update = "update users set user_balance=$user_balance - $price where user_id='$user_id'";
to
$update = "update users set user_balance = user_balance - $price WHERE user_id='$user_id'";
or with keeping your variable
$user_balance = $user_balance - $price;
$update = "UPDATE users SET user_balance = '$user_balance' WHERE user_id='$user_id'";
and remove if you choose first solution
$user_balance = $user_balance - $price;
also your previous query is overidden and won't be executed
this one
$update = "update users set posts='yes' where user_id='$user_id'";