{"id":785,"date":"2020-11-18T12:34:27","date_gmt":"2020-11-18T12:34:27","guid":{"rendered":"https:\/\/www.onfocus.co.uk\/?p=785"},"modified":"2020-11-25T09:12:36","modified_gmt":"2020-11-25T09:12:36","slug":"500-error-with-pdo-statement","status":"publish","type":"post","link":"https:\/\/www.onfocus.co.uk\/blog\/500-error-with-pdo-statement\/","title":{"rendered":"500 Error with PDO Statement"},"content":{"rendered":"\n<p>I kept getting a 500 Error message after creating a new php script to process a form from a web page.<\/p>\n\n\n\n<p>The script was to process a set of instructions before inserting data in to a MYSQL database table.<\/p>\n\n\n\n<p>I had already debugged my main php instructions but could still not get to work and insert the data &#8211; I just kept getting the page displaying &#8216;500 Error&#8217; \ud83d\ude41<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"560\" height=\"120\" src=\"https:\/\/www.onfocus.co.uk\/blog\/wp-content\/uploads\/2020\/11\/500-error.png\" alt=\"\" class=\"wp-image-786\" srcset=\"https:\/\/www.onfocus.co.uk\/blog\/wp-content\/uploads\/2020\/11\/500-error.png 560w, https:\/\/www.onfocus.co.uk\/blog\/wp-content\/uploads\/2020\/11\/500-error-300x64.png 300w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/figure>\n\n\n\n<p>To help me fix the error I needed to show details or something to point me in the right direction.<\/p>\n\n\n\n<p>For my purpose I decided to put the try\/catch method on the page in order to attain the exception.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<p><strong>try {<\/strong><\/p>\n\n\n\n<p><strong>$stmt= $pdo-&gt;prepare($sql);<\/strong><\/p>\n\n\n\n<p><strong>$stmt->execute($data);<\/strong><\/p>\n\n\n\n<p><strong>} catch (PDOException $e) {<\/strong><\/p>\n\n\n\n<p><strong>echo &#8216;Error Message: &#8216; . $e-&gt;getMessage();<\/strong><\/p>\n\n\n\n<p><strong>}<\/strong><\/p>\n\n\n\n<p>When running the script once more it revealed more details <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Connection failed: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined<\/strong><\/pre>\n\n\n\n<p>This was enough for me to learn that I had a 500 Error because the number of elements in my PDO $data <code>values<\/code> was not the same as my insert $sql <code>element<\/code>. I in fact had one extra value that was throwing the error.<\/p>\n\n\n\n<p>It was something so simple to fix but had taken me a long time to find!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#46;&#46;&#46;<\/p>\n","protected":false},"author":3,"featured_media":791,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[50,49,48],"class_list":["post-785","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website-design","tag-mysql","tag-pdo","tag-php"],"_links":{"self":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=785"}],"version-history":[{"count":5,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/785\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/posts\/785\/revisions\/806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media\/791"}],"wp:attachment":[{"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onfocus.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}