php 如何提取余额并与输入进行比较?

atmip9wb  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(109)

我想比较用户余额从数据库和输入金额。如果用户余额低于(整数)10在数据库中。和形式输入是(整数)12。
在下面的代码中,我希望发送者看到“余额不足”,如果他没有足够的余额输入的形式;

public function makeTransactions($from, $amount, $to) {
    try {
        $check = $this->db->prepare("SELECT balance FROM `Users` WHERE `email` = '$from' "); 
        $check->execute();
        $available = $check->fetch(PDO::FETCH_ASSOC);

        if ($available < $amount) {
            $msg = "Insufficient balance!";
            return $msg;
        } else {
            $sendNow = $this->db->prepare("UPDATE `Users` SET balance = balance-'$amount' WHERE `email` = '$from' ");
            $proceed = $sendNow->execute(); 

            if ($proceed) {
                $proceed = $this->db->prepare("UPDATE `Users` SET balance = balance+'$amount' WHERE `email` = '$to' ");
                $result = $proceed->execute(); 

                if ($result) {
                    $msg = "Transaction successful";
                    return $msg;
                } else {
                    $msg = "Transaction failed";
                    return $msg;
                }
            }
        }
    }
}

相关问题