两个变量之间的减法不起作用,如何修复?

zf9nrax1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(406)

所以,我试图减去2个变量,$price到$user\u balance(插入post时,我想要(new)$user\u balance=(current)$user\u balance-$price,但它不是减法,如何修复它(这个不起作用的代码在exit()之上)
例子:
(余额在用户表中)
(posts进入posts表)
将post引入数据库前的余额:100
数据库中引入的产品价格:50
引入产品价格后在数据库中的余额应该是100-50,所以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();
    }
}

}

ao218c7q

ao218c7q1#

你好像做了两次减法
改变

$update = "update users set user_balance=$user_balance - $price where user_id='$user_id'";

$update = "update users set user_balance = user_balance - $price WHERE user_id='$user_id'";

或者保留变量

$user_balance = $user_balance - $price;
$update = "UPDATE users SET user_balance = '$user_balance' WHERE user_id='$user_id'";

如果你选择第一种解决方案

$user_balance = $user_balance - $price;

另外,您以前的查询被过度隐藏,将不会执行
这个

$update = "update users set posts='yes' where user_id='$user_id'";

相关问题