I have a problem when comparing two PHP values. The first value is being passed as a parameter in the query string, while the second parameter is a result of an sql query.
The logic I need here is to compare both values, so that if the user has enough credit, his payment can be processed, but otherwise, an error message is shown.
This is my code:
public function storeNewTicket($userId, $amount, $validity) {
$currentBalance = $this->getBalance($userId);
$floatCurrentBalance = floatval($currentBalance);
$floatAmount = floatval($amount);
if ($floatCurrentBalance > $floatAmount) {
/*Proceed with the insert*/
}
else {
echo "You don't have enough credit to process this payment."
}
}
In a seperate function in the same file called getBalance(), I am getting the current balance of the user from a different table and returning it as an object. I am 100% sure that this is working. Here is the code:
public function getBalance($userId) {
$stmt = $this->conn->prepare("SELECT balance FROM userbank WHERE UserId = ?");
$stmt->bind_param("s", $userId);
if ($stmt->execute()) {
$bal = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $bal['balance'];
} else {
return NULL;
}
}
The code is always going through the else clause and echoing: You don't have enough credit to process this payment.
;
Can someone please help me understand how to convert the variables to decimals or floats and compare them?