0

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?

Mark
  • 2,961
  • 1
  • 16
  • 24
  • 2
    are exceptions set? http://php.net/manual/en/pdo.error-handling.php – Funk Forty Niner Jan 10 '19 at 21:03
  • 1
    Put the `prepare()` inside the `try` block too. – Alex Howansky Jan 10 '19 at 21:05
  • Yes, thank you. Please feel free to put those comments as answers, and I will upvote them. These both helped to fix my issue. – Mark Jan 10 '19 at 21:07
  • 1
    In our applications we often wrap the entire script in a big `try` block, so any PDO exception anywhere will abort it. – Barmar Jan 10 '19 at 21:12
  • 1
    Thanks Mark but personally, feel it should be *(either)* a community wiki answer, not something that rep should be made from comments left by @AlexHowansky and I. Edit: *Or* closed as a duplicate of [My PDO Statement doesn't work](https://stackoverflow.com/q/32648371/). – Funk Forty Niner Jan 10 '19 at 21:41
  • @FunkFortyNiner Hmm, on the link you mentioned, there is no indication of try-catch. I'm not sure if it is a typical duplicate. – Mark Jan 10 '19 at 22:13

1 Answers1

1

2 points.

First is pointed by @Funk Forty Niner. By default PDO send warning, not exception. You must set the error mode :

$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

Second: use the "named parameters" not the "columns name" for the array in your execute method

$statement->execute([
        ':requestor' => $requestor_id,
        ':location' => $request_location
]);
acucchieri
  • 562
  • 4
  • 8
  • Using `:` in keys when binding parameters is optional. This for good reasons - so we can just bind with `->execute($data)` without having to transform the keys. – Paul Spiegel Mar 07 '19 at 11:25