I have the following functions.
function generateOTP($theUser, $thePhone){
global $pdo;
$stmt = $pdo->prepare("DELETE FROM otp WHERE phone = :phone");
$stmt-> bindValue(':phone', $thePhone);
$stmt-> execute();
$stmt = $pdo->prepare("INSERT INTO otp(phone, otp, type, validity)VALUES(:phone, :otp, :type, :val)");
$stmt-> bindValue(':phone', $thePhone);
$stmt-> bindValue(':otp', rand(1000, 9999));
$stmt-> bindValue(':type', 'new');
$stmt-> bindValue(':val', date('Y-m-d H:i:s', strtotime('+5 mins')));
$stmt-> execute();
}
function sendOTP($theUser, $thePhone){
global $pdo;
generateOTP($theUser, $thePhone);
}
// CALLED SOMEWHERE LIKE THIS
if(sendOTP($theUser, $thePhone){
echo "OTP SENT";
}else{
echo "OTP SENDING FAILED";
}
The problem I have encountered is that even if the sendOTP()
function is executed well and all the records have been inserted in the database, it always moves to the else
block and prints OTP SENDING FAILED
. In other words, it always returns false assuming that the function failed to execute successfully. But actually, function has been executed well and all the queries are executed properly. This is a strange issue I have never come across before. How can I solve this?