douzhao7445 2018-09-04 20:58
浏览 63
已采纳

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

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();
    }
}

}

  • 写回答

1条回答 默认 最新

  • doubishi8303 2018-09-04 21:24
    关注

    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'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度