Undefined index in PHP

Dear All

I am using below script on my web & its working fine, but in my local PC (xampp), when I test this script showing the followinh error.

Notice: Undefined index: factory in D:\xampp\htdocs\xampp\mehidy\ano5.php on line 49

Notice: Undefined index: fdepartment in D:\xampp\htdocs\xampp\mehidy\ano5.php on line 50

Notice: Undefined index: designation in D:\xampp\htdocs\xampp\mehidy\ano5.php on line 51

Notice: Undefined index: mrchand_name in D:\xampp\htdocs\xampp\mehidy\ano5.php on line 52

Notice: Undefined index: ac in D:\xampp\htdocs\xampp\mehidy\ano5.php on line 64

Please help me to find out the reason.


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="decorous_ord"; // Database name

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

echo "";


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
    .errText {
        font-family: Arial;
        font-size: 13px;
        color: #CC0000;
        text-decoration: none;
        font-weight: BOLD;

      font-size: 17px;
      font-weight: BOLD;

  <title>Add new order</title>
  <body bgcolor="#FFFAFA">

      $factory = $_POST['factory'];
      $fdepartment = $_POST['fdepartment'];
      $designation = $_POST['designation'];
      $mrchand_name = $_POST['mrchand_name'];
      $errUniqkey = "";
       $mrchand_name = isset($_POST['mrchand_name']) ? $_POST['mrchand_name'] : false;
       $secret_code = isset($_POST['secret_code']) ? $_POST['secret_code'] : false;

      $errors       = "";
      $displayform = '1';

      if($_POST["ac"]=="login"){  // start of login if
        // Full Name must be letters, dash and spaces only
        if (!$factory) {
        $errFactory = '<p class="errText">Please select your Factoy.</p>';
         $error = 'YES';

        if (!$fdepartment) {
        $errFdepartment = '<p class="errText">Please select your Department.</p>';
         $error = 'YES';

        if (!$designation) {
        $errDesignation = '<p class="errText">Please select your Designation.</p>';
         $error = 'YES';

        if (!$mrchand_name) {
        $errMrchand_name = '<p class="errText">Please select your Name.</p>';
         $error = 'YES';

    if ($mrchand_name && $secret_code) {
        //if ($gender == 'male' && strtolower($title) != 'mr') {
       if (($mrchand_name == 'Mr.Rony' && strtolower($secret_code) != 'rony') || ($mrchand_name == 'Mr.Rayhan' && strtolower($secret_code) != 'rayhan')|| ($mrchand_name == 'Mr.Mehidy Hassan' && strtolower($secret_code) != 'mehidy')){
            $errSecret_code = '<p class="errText">Your secret code is not matching with your Name.</p>';
        $error = 'YES';

         if(preg_match("/^[0-9\\"\\']+$/", $_POST["uniqkey"]) === 0){
          $errUniqkey = '<p class="errText">Uniq key must be in digits</p>';
          $error = 'YES';

        if ($error != 'YES'){ // only insert data if no errors
           $displayform = '0'; // disable form show
           $factory = mysql_real_escape_string($_POST['factory']);
           $fdepartment = mysql_real_escape_string($_POST['fdepartment']);
           $designation = mysql_real_escape_string($_POST['designation']);
           $mrchand_name = mysql_real_escape_string($_POST['mrchand_name']);
           $secret_code = mysql_real_escape_string($_POST['secret_code']);
           $uniqkey = mysql_real_escape_string($_POST['uniqkey']);

             	if(!$secret_code ){
				//if any weren't display the error message
				echo "<center>You need to fill in all of the required (* marked) fields!</center>";

           $sql = mysql_query("INSERT INTO newcomp
                                       SET   factory = '$factory',
                                             fdepartment = '$fdepartment',
                                             designation = '$designation',
                                             mrchand_name = '$mrchand_name',
                                             secret_code = '$secret_code',
                                             uniqkey = '$uniqkey'

          if (!$sql) {
           echo 'Mysql error, data not saved, try again -' . mysql_error();
           $displayform = '1'; // show form to try again

             die('Your data was inserted <a href="vod.php">Click Here to View Update Details</a> <a href="ano.php">Click Here to Add New Orders</a>');  }


        } // end of insert if
      } // end of  login if

  if ($displayform = '1') { // only show form if required

  <form name="main" action="<?php $PHP_SELF ?>" method="post">
    <input type="hidden" name="ac" value="login" />
    <table width="1200" border="0" cellpadding="4" cellspacing="0" bordercolor="#000000" bgcolor="#EDEFF1">

      <tr align="center">
        <td colspan="6"><strong style="font-size:18px">Add New Order</strong></td>
<tr align="center" bgcolor="#FD9003">
        <td colspan="6" bgcolor="#A6B39D"><span class="preppy">Factory Details</span></td>

      <tr align="center" bgcolor="#FD9003">
        <td colspan="6" bgcolor="#FFE4E1">
          <?php  if(isset($errFactory)) echo $errFactory; ?>
          <?php  if(isset($errFdepartment)) echo $errFdepartment; ?>
          <?php  if(isset($errDesignation)) echo $errDesignation; ?>
          <?php  if(isset($errMrchand_name)) echo $errMrchand_name; ?>
          <?php  if(isset($errSecret_code)) echo $errSecret_code; ?>
          <?php  if(isset($errUniqkey)) echo $errUniqkey; ?>



<th>Factory Name<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;"> </font></th>
<th>Department<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;"> </font></th>
<th>Designation<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;"> </font></th>
<th>Merchandiser Name<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;"> </font></th>
<th>Secret Code<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;">Secret Code is Your Identity</font></th>
<th>Unique Key<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color:#0101DF; font-size: 75%;">


$query = mysql_query("SELECT MAX(uniqkey) FROM `newcomp`");
$results = mysql_fetch_array($query);
$un = $results['MAX(uniqkey)'] + 1;

echo "$un";




<td align="center">
<select name="factory" value="<?php echo $factory;?>">
<option value="Crosswear Ind Ltd" <?php if($factory=="Crosswear Ind Ltd") echo(" selected=\\"selected\\"");?> >Crosswear Ind Ltd</option>

<td align="center">
<select name="fdepartment" value="<?php echo $fdepartment;?>">
<option value="Merchandising" <?php if($fdepartment=="Merchandising") echo(" selected=\\"selected\\"");?> >Merchandising</option>

<td align="center">
<select name="designation" value="<?php echo $designation;?>">
<option value="">Select...</option>
<option value="Merchandising Manager" <?php if($designation=="Merchandising Manager") echo(" selected=\\"selected\\"");?> >Merchandising Manager</option>
<option value="Merchandising Inchagr"<?php if($designation=="Merchandising Inchagr") echo(" selected=\\"selected\\"");?> >Merchandising Inchagre</option>
<option value="Sr.Merchandiser"<?php if($designation=="Sr.Merchandiser") echo(" selected=\\"selected\\"");?> >Sr.Merchandiser </option>
<option value="Merchandiser"<?php if($designation=="Merchandiser") echo(" selected=\\"selected\\"");?> >Merchandiser </option>
<option value="Trainee Merchandiser"<?php if($designation=="Trainee Merchandiser") echo(" selected=\\"selected\\"");?> >Trainee Merchandiser </option>


<td align="center">
<select name="mrchand_name" value="<?php echo $mrchand_name;?>">
<option value="">Select...</option>
<option value="Mr.Sultan Mahmud" <?php if($mrchand_name=="Mr.Sultan Mahmud") echo(" selected=\\"selected\\"");?> >Mr.Sultan Mahmud</option>
<option value="Mr.Kajal Pasha"<?php if($mrchand_name=="Mr.Kajal Pasha") echo(" selected=\\"selected\\"");?> >Mr.Kajal Pasha</option>
<option value="Mr.Mehidy Hassan"<?php if($mrchand_name=="Mr.Mehidy Hassan") echo(" selected=\\"selected\\"");?> >Mr.Mehidy Hassan</option>
<option value="Mr.Rayhan"<?php if($mrchand_name=="Mr.Rayhan") echo(" selected=\\"selected\\"");?> >Mr.Rayhan </option>
<option value="Mr.Bikash"<?php if($mrchand_name=="Mr.Bikash") echo(" selected=\\"selected\\"");?> >Mr.Bikash </option>
<option value="Mr.Rony"<?php if($mrchand_name=="Mr.Rony") echo(" selected=\\"selected\\"");?> >Mr.Rony </option>

<td align="center">
<input type="password" name="secret_code" value="<?php echo $_POST[""];?>" size="15" /></td>

<td align="center">
		<input type="text" name="uniqkey"  value="<?php echo $_POST["uniqkey"];?>" size="15" />

<tr align="center" bgcolor="#FD9003">
<td colspan="6" bgcolor="#FFE4E1">   </td>
       <tr align="center" bgcolor="#FD9003">
        <td colspan="6" bgcolor="#A6B39D"> </td>

<tr align="center" bgcolor="#FD9003">
<td colspan="6" bgcolor="#FFE4E1">   </td>

      <tr align="center">
        <td colspan="6"><input type="submit" name="Submit" value="Submit"/> <input type="reset" name="reset" value="Reset"/></td>

Developed by <a href="http://www.mehidy.com" target="_blank">Mohammad Mehidy Hassan</a><br/>Live on: mehiddy@hotmail.com

  } // end of form display if

The first time the script is executed (before the form has been sent), $_POST doesn’t contain all those fields.
The reason you don’t see those Notices on your website, and you do see them on your local machine, has to do with the error settings. Obviously on your local machine PHP has been set to show Notices, and on your webhost it hasn’t.

You could change the setting (I don’t know how), or you could change your code so it checks if a $_POST variable exists before using it.


Thanks for your response. Kindly show me an example, how can I change the code.

The other way is to put this before the line 49 and the post variables are read:

$factory = '';
      $fdepartment = '';
      $designation = '';
      $mrchand_name = '';

For example using the ternary operator (see example #2):

$factory = (isset($_POST['factory'])) ? $_POST['factory'] : '';

This will check if the variable exists: (isset($_POST[‘factory’]))
If it does, it will return the value after the ‘?’ : $_POST[‘factory’]
If it doesn’t, it will return the value after the ‘:’ : ‘’;
So after this line, $factory will exist.

And the IF can become:

      if (isset($_POST['ac']) && $_POST['ac'] == 'login') {  // start of login if 

First it checks if the variable exists, and only if it exists will it check its value.

Thank you very much,

its working fine.

Sorry…now facing new problem as below

Undefined variable: error in D:\xampp\htdocs\xampp\mehidy\ano5.php on line

if ($error != ‘YES’){

Undefined index at line this

<input type=“text” name=“uniqkey” value=“<?php echo $_POST[“uniqkey”];?>” size=“15” />

You don’t initialize $error anywhere, so if there’s no errors, the variable $error doesn’t exist.

This is the same problem as before. Check if the $_POST variable exists before using it.

Probably it’s “Undefined Index”.
You must have seen it in some of the PHP notices or warnings.

It comes, when you are working with arrays and the Index or Key your code is using doesn’t actually exist.

For Example,
if you define an array:

$a = array( ‘a’=>‘pak’ , ‘b’ => ‘asif’ );

having two Indexes (a & b),

But later on, you code like:


Now, c index doesn’t actually existed in $a array, so a PHP warning / notice will appear on run time saying:

Read more: http://wiki.answers.com/Q/What_is_an_unidentified_index_in_php#ixzz1d77H58bn