{"id":1026,"date":"2016-08-25T06:16:32","date_gmt":"2016-08-25T06:16:32","guid":{"rendered":"http:\/\/www.icantinternet.org\/?p=1026"},"modified":"2016-08-25T06:48:38","modified_gmt":"2016-08-25T06:48:38","slug":"php-notice-undefined-index","status":"publish","type":"post","link":"https:\/\/beheydt.be\/en\/php-notice-undefined-index\/","title":{"rendered":"PHP Notice: Undefined index"},"content":{"rendered":"<p>Have you ever gotten this error:<br \/>\n<em>Notice: Undefined index &#8216;fields of the table&#8217; in &#8216;path of the PHP file you&#8217;re executing&#8217; <\/em>on line &#8216;the current linenumber&#8217; ? I think you do, since most probably, Google or Bing or so have lead you to this post because you were searching for a solution for that very errormessage \ud83d\ude42<br \/>\nWell, you get this error when you are using  variables in an array, but the &#8220;index&#8221; is not set.<br \/>\nAn array is basically a &#8220;group&#8221; of keys and values put together in one variable, and can be used with that variable&#8217;s name, and the index (or key) you are refering to. For instance:<\/p>\n<p><em><\/p>\n<p><?php\n$MyArray = array(\n    \"key1\" => &#8220;value1&#8221;,<br \/>\n    &#8220;key2&#8221; => &#8220;value2&#8221;,<br \/>\n    &#8220;key3&#8221; => &#8220;value3&#8221;<br \/>\n);<\/p>\n<p>\/\/ Or the same array can be written as such:<br \/>\n$MyArray = [<br \/>\n    &#8220;key1&#8221; => &#8220;value1&#8221;,<br \/>\n    &#8220;key2&#8221; => &#8220;value2&#8221;,<br \/>\n    &#8220;key3&#8221; => &#8220;value3&#8221;<br \/>\n];<br \/>\n?><\/p>\n<p><\/em> <\/p>\n<p>If you need the value of key2, you will refer to it as:<\/p>\n<p><em><br \/>\n$MyArray[&#8220;key2&#8221;];<br \/>\n<\/em><\/p>\n<p>Now, if you do not use a &#8220;key&#8221; while assigning values to your array, PHP will define the keys by itself, in an ordered numerical list, starting from &#8220;0&#8221;, resulting in this:<\/p>\n<p><em><br \/>\n<?php\n\n$MyArray = [\n    \"value1\",\n    \"value2\",\n    \"value3\"\n];\n\necho $MyArray[1];\n\/\/this will print \"value2\" to the screen.\n\n?><\/p>\n<p><\/em> <\/p>\n<p>Quite a common reason to get this error is when using $_POST or $_GET arrays to retrieve the data from a form, and the variables are not set.<\/p>\n<p>To get rid of this error, it is sufficient to test if the field of the table (so, the array) was initialized with the function isset ().<\/p>\n<p><em>\/\/ Check this before you start using your $_POST[&#8216;value&#8217;] variable<\/em><br \/>\n<em>if (isset($_POST[&#8216;value&#8217;]))<\/em><br \/>\n<em>{<\/em><br \/>\n<em> \/\/ Use and abuse your $_POST[&#8216;value&#8217;] here!<\/em><br \/>\n<em>}<\/em><\/p>\n<p>Another way to get rid of this message, is by changing the configuration of your Apache\/PHP setup. The error above is considered a minor error, corresponding to the constant E_NOTICE, and thus not shown in the standard configuration. But be warned, this doesn&#8217;t FIX the problem, it only stops showing it to you.<br \/>\nYou can set your error reporting level in your PHP scripts with the error_reporting function, and by using these constants:<br \/>\nE_WARNING \u00a0\u00a0\u00a0 Non-fatal run-time errors. Execution of the script is not halted<br \/>\nE_NOTICE \u00a0\u00a0\u00a0 Run-time notices. The script found something that might be an error, but could also happen when running a script normally<br \/>\nE_RECOVERABLE_ERROR \u00a0\u00a0\u00a0 Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle<br \/>\nE_ALL<br \/>\nErrorlevels can be combined in the error_reporting function such as E_ALL^E_NOTICE, which would show all errors, except Notices.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever gotten this error: Notice: Undefined index &#8216;fields of the table&#8217; in &#8216;path of the PHP file you&#8217;re executing&#8217; on line &#8216;the current linenumber&#8217; ? I think you do, since most probably, Google or Bing or so have lead you to this post because you were searching for a solution for that very [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[942],"tags":[624,875],"class_list":["post-1026","post","type-post","status-publish","format-standard","hentry","category-webdevelopment-icantinternet-en","tag-php","tag-webdevelopment"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbkKxJ-gy","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/1026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/comments?post=1026"}],"version-history":[{"count":0,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/posts\/1026\/revisions"}],"wp:attachment":[{"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/media?parent=1026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/categories?post=1026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beheydt.be\/en\/wp-json\/wp\/v2\/tags?post=1026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}