My website sends an XMLHttpRequest
to this PHP script:
<?php
if(isset($_POST))
{
require ('../../PDOConnect.php');
try
{
$insert = $pdo->prepare("INSERT INTO priorities
employeeID, WorkOrderNumber
VALUES (?,?)");
$insert->bindValue(1,$_POST['employeeID'],PDO::PARAM_INT);
$insert->bindValue(2,$_POST['WorkOrderNumber'],PDO::PARAM_INT);
$insert->execute();
echo "try";
}
catch(Exception $e)
{
echo "catch";
echo $e;
}
}
?>
At this point, the priorities
table does not exist. I'm expecting the XMLHttpRequest
's status
to be 500
because the query obviously will always fail. PDO does log the error because PDOConnect contains
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
but I get a 200
, not a 500
. I'm new to PDO. When I was using MySQLi prepared statements, I would get a 500
back from failed PHP scripts, but PDO acts differently. I want the client to know that the query failed, rather than getting a 200
back and thinking that everything went okay. I get the same results with and without try
and catch
. Am I failing to understand how PDO works? Do I need to change a PDO setting?
Edit: I added echo "try";
and echo "catch";
. The client's responseText
is always "try", never "catch". It seems that the catch
statement doesn't run. Why is that?
My PDOConnect.php
file contains:
<?php
$options =
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
];
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4','root','mypassword',$options);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
?>