{"id":1166,"date":"2022-08-30T15:13:29","date_gmt":"2022-08-30T15:13:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/notice-undefined-index-for-all-fields-in-my-database-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:29","modified_gmt":"2022-08-30T15:13:29","slug":"notice-undefined-index-for-all-fields-in-my-database-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/notice-undefined-index-for-all-fields-in-my-database-collection-of-common-programming-errors\/","title":{"rendered":"Notice: Undefined index for all fields in my database-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>Use <code>isset<\/code> to check if they exist before using any index on <code>$_POST<\/code> or <code>$_GET<\/code>.<\/p>\n<pre><code>if (isset($_GET['id'])) {\n  $results-&gt;execute(array(\n    ':id' =&gt; $_GET['id'],\n  ));\n}\n\n...\n\n':id' =&gt; isset($_POST['id']) ? $_POST['id'] : '',\n':firstname' =&gt; isset($_POST['firstname']) ? $_POST['firstname'] : '',\n\/\/ and so on\n...\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You code does not make any sense: When <code>$_POST['submit'] )<\/code> is set, you redirect to another page so the code that uses the <code>POST<\/code> variables will run when <code>$_POST['submit'] )<\/code> is not set.<\/p>\n<p>This is what it looks like to php:<\/p>\n<pre><code>if (isset($_POST['submit'] ))\n{\n   header(\"Location: template\/header.php\");\n}\n\n$update = $dbh-&gt;prepare('UPDATE details SET firstname = :firstname, surname = :surname, houseno = :houseno, street = :street, town= :town,\n       county = :county, postcode = :postcode,  mobile = :mobile, nickname = :nickname, website = :website,\n       homephone = :homephone WHERE id = :id');\n$update-&gt;execute(array(\n    ':id' =&gt; $_POST['id'],\n    ':firstname' =&gt; $_POST['firstname'],\n    ':surname' =&gt; $_POST['surname'],\n    ':houseno' =&gt; $_POST['houseno'],\n    ':street' =&gt; $_POST['street'],\n    ':town' =&gt; $_POST['town'],\n    ':county' =&gt; $_POST['county'],\n    ':postcode' =&gt; $_POST['postcode'],\n    ':mobile' =&gt; $_POST['mobile'],\n    ':nickname' =&gt; $_POST['nickname'],\n    ':website' =&gt; $_POST['website'],\n    ':homephone' =&gt; $_POST['homephone'],    \n));\n\n$row = $results-&gt;fetch();\n<\/code><\/pre>\n<p>What exactly is the <code>header<\/code> call for?<\/p>\n<p>By the way, you probably want something like this:<\/p>\n<pre><code>require 'server.php';\n\nif (isset($_POST['submit'] ))\n{\n  $update = $dbh-&gt;prepare('UPDATE details SET firstname = :firstname, surname = :surname, houseno = :houseno, street = :street, town= :town,\n       county = :county, postcode = :postcode,  mobile = :mobile, nickname = :nickname, website = :website,\n       homephone = :homephone WHERE id = :id');\n  $update-&gt;execute(array(\n    ':id' =&gt; $_POST['id'],\n    ':firstname' =&gt; $_POST['firstname'],\n    ':surname' =&gt; $_POST['surname'],\n    ':houseno' =&gt; $_POST['houseno'],\n    ':street' =&gt; $_POST['street'],\n    ':town' =&gt; $_POST['town'],\n    ':county' =&gt; $_POST['county'],\n    ':postcode' =&gt; $_POST['postcode'],\n    ':mobile' =&gt; $_POST['mobile'],\n    ':nickname' =&gt; $_POST['nickname'],\n    ':website' =&gt; $_POST['website'],\n    ':homephone' =&gt; $_POST['homephone'],    \n  ));\n\n  \/\/ show message or something\n}\nelse\n{\n  \/\/ normal get request, nothing posted\n  $results = $dbh-&gt;prepare('SELECT * FROM details WHERE id = :id');\n  $results-&gt;execute(array(':id' =&gt; $_GET['id'],\n     ));\n  $row = $results-&gt;fetch();\n\n  \/\/ show rest of the page\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:33:59. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Use isset to check if they exist before using any index on $_POST or $_GET. if (isset($_GET[&#8216;id&#8217;])) { $results-&gt;execute(array( &#8216;:id&#8217; =&gt; $_GET[&#8216;id&#8217;], )); } &#8230; &#8216;:id&#8217; =&gt; isset($_POST[&#8216;id&#8217;]) ? $_POST[&#8216;id&#8217;] : &#8221;, &#8216;:firstname&#8217; =&gt; isset($_POST[&#8216;firstname&#8217;]) ? $_POST[&#8216;firstname&#8217;] : &#8221;, \/\/ and so on &#8230; You code does not make any sense: When $_POST[&#8216;submit&#8217;] ) is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1166","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1166"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1166\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}