500 Error with PDO Statement

I kept getting a 500 Error message after creating a new php script to process a form from a web page.

The script was to process a set of instructions before inserting data in to a MYSQL database table.

I had already debugged my main php instructions but could still not get to work and insert the data – I just kept getting the page displaying ‘500 Error’ 🙁

To help me fix the error I needed to show details or something to point me in the right direction.

For my purpose I decided to put the try/catch method on the page in order to attain the exception.

I did this by enclosing the PDO statement prepare and execute within the try{} and put the exception that was causing the 500 Error in to the catch{} as below:

try {

$stmt= $pdo->prepare($sql);

$stmt->execute($data);

} catch (PDOException $e) {

echo ‘Error Message: ‘ . $e->getMessage();

}

When running the script once more it revealed more details

Connection failed: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

This was enough for me to learn that I had a 500 Error because the number of elements in my PDO $data values was not the same as my insert $sql element. I in fact had one extra value that was throwing the error.

It was something so simple to fix but had taken me a long time to find!

You may also like...