我想比较用户余额从数据库和输入金额。如果用户余额低于(整数)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;
}
}
}
}
}
1条答案
按热度按时间wlwcrazw1#
尝试用途:
https://www.php.net/manual/en/pdostatement.fetchcolumn.php