Posted by: phillipnb | November 26, 2010

Undefined Index and $_GET

This month I have been fighting with different types of php errors but the one that caught my attention was: Notice: Undefined index:. The initial reaction to this error from one of my friends was to change the settings in the php.ini file to prevent php from spitting out these type of notices. Honestly, that is not the right way to go.

I looked into the form that was causing this error. The form tag and the associated code looked like this:

form name='form1' method='post' action='processForm.php'
Enter your first name input type='text' value='' name='name'

The html code for this form looks ok. In that case, what is causing this error?. The next place to look what was causing this error was to look into processForm.php The first line in processForm.php is,

$var1 = $_GET['name'];

If we look at the explanation for $_GET in the php manual,it says “An associative array of variables passed to the current script via the URL parameters”. The word “associative array” clearly tells us what this error is. If we access any array variable which is not set, it will throw an error saying that the index is undefined. Is this argument valid in our case? Let us test it. Let us rewrite that code by checking if that form variable is set and then try to access that variable:

if (isset($_GET['name']))
$var1 = $_GET['name'];

When we run this code we can find that php does not complain any more about the ‘Undefined Index’ error. So, what is the moral of this story? – Never access a form variable using $_GET,$_POST or $_REQUEST without the checking whether it is set using isset(). Always check if a form variable is set and then access that variable. So, if you have fifteen or twenty variables in a form, you ought to check each and every variable using the isset() before you use the value from that form variable – absolutely no shortcuts.

Till Next Time
Happy PHP Programming



  1. i have this problem, please help me 😥

    $id_pegawai = $_GET[‘id_pegawai’];
    $query_pegawai = “SELECT * FROM data_pegawai WHERE id_pegawai=’$id_pegawai'”;
    $hasil = mysql_query($query_pegawai,$connect);
    $data = mysql_fetch_array($hasil);

    and i have this report (error) :

    Notice: Undefined index: id_pegawai in C:\wamp\www\dpchandra\edit.php on line 10

    • Where is Line 10?

  2. thank’s !!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: