I have a simple script that works ideally under normal conditions. I'm trying to get it to throw an exception, for testing purposes. This is a PHP PDO insert. I'd like for it to throw an exception if a new row is not inserted.
Here is my code:
$query = "INSERT INTO reservations (requestor, location)
VALUES (:requestor, :location)";
$statement = $pdo->prepare($query);
try {
$statement->execute([
'requestor' => $requestor_id,
'location' => $request_location
]);
echo "Success.";
}
catch(Exception $e) {
echo "Sorry, but your request was not completed properly.<br>";
echo "Message: " . $e->getMessage();
}
This works perfectly. But if I play with the code to make it not work, by changing the table column names or bound parameters, it still gives a success message. I'm not getting a new row inserted, as expected. See below:
$query = "INSERT INTO reservations (requestorzzz, locationzzz)
VALUES (:requestor, :location)";
$statement = $pdo->prepare($query);
try {
$statement->execute([
'requestorzzz' => $requestor_id,
'locationzzz' => $request_location
]);
echo "Thank you! Your request was received.";
}
catch(Exception $e) {
echo "Sorry, but your request was not completed properly.<br>";
echo "Message: " . $e->getMessage();
}
I do get php error messages, but the exception is not thrown. No new column is created (naturally), but I need it to go to the catch to notify users that their request didn't happen. Is there something I'm doing incorrectly?