SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    <?php 
     
    // PUT YOUR DATABASE CONNECTION SCRIPT HERE 
    $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"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    
    
    <head>
    <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;  
            }  
    
        .preppy{
          color:#000088;
          font-size: 17px;  
          font-weight: BOLD;  
           }
    
    
      </style>  
      <title>Add new order</title>  
      </head> 
      <body bgcolor="#FFFAFA">   
    
      <?php 
          $factory = $_POST['factory'];
          $fdepartment = $_POST['fdepartment'];
          $designation = $_POST['designation'];
          $mrchand_name = $_POST['mrchand_name'];
          $errSecret_code="";
          $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>";
    			}
           else{
    
               $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 
              }else{ 
                  
                 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 
     
      ?>  
      <center>  
      <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>  
    <tr align="center" bgcolor="#FD9003">  
            <td colspan="6" bgcolor="#A6B39D"><span class="preppy">Factory Details</span></td>  
          </tr>  
               
          <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; ?>  
      
            
            </td>  
          </tr>  
    
    
    
    <tr>
    
    <th>Factory Name<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;">&nbsp;</font></th>
    <th>Department<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;">&nbsp;</font></th>
    <th>Designation<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;">&nbsp;</font></th>
    <th>Merchandiser Name<font style="color: #800517; font-size: 105%;"><b/>*</font><br/><font style="color: #666666; font-size: 60%;">&nbsp;</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%;">
    
    <?php
            
    $query = mysql_query("SELECT MAX(uniqkey) FROM `newcomp`");
    $results = mysql_fetch_array($query);
    $un = $results['MAX(uniqkey)'] + 1;
    
    echo "$un";
    
    ?>
    
    </font></th>
    
    </tr>
    <tr>
    
    
    <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>
    </select>          
    </td>
    
    <td align="center">
    <select name="fdepartment" value="<?php echo $fdepartment;?>">
    <option value="Merchandising" <?php if($fdepartment=="Merchandising") echo(" selected=\"selected\"");?> >Merchandising</option>
    </select>          
    </td>
    
    <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>
    
    </select>          
    </td>
    
    <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>
    
    </select>          
    </td>
    <td align="center">
    <input type="password" name="secret_code" value="<?php echo $_POST[""];?>" size="15" /></td>
    </td>
    
    <td align="center">
    		<input type="text" name="uniqkey"  value="<?php echo $_POST["uniqkey"];?>" size="15" />
    </td>
    
    </tr>
    <tr align="center" bgcolor="#FD9003">  
    <td colspan="6" bgcolor="#FFE4E1">   </td>  
    </tr>  
           <tr align="center" bgcolor="#FD9003">  
            <td colspan="6" bgcolor="#A6B39D">&nbsp;</td>  
          </tr>  
    
    <tr align="center" bgcolor="#FD9003">  
    <td colspan="6" bgcolor="#FFE4E1">   </td>  
    </tr>  
    
          <tr align="center">
            <td colspan="6"><input type="submit" name="Submit" value="Submit"/> <input type="reset" name="reset" value="Reset"/></td>
          </tr>  
    </table>
     </form>
      
    Developed by <a href="http://www.mehidy.com" target="_blank">Mohammad Mehidy Hassan</a><br/>Live on: mehiddy@hotmail.com  
          
        
      </center>  
    <?php  
      } // end of form display if 
    ?> 
      </body>  
    </html>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear

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

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    The other way is to put this before the line 49 and the post variables are read:
    PHP Code:
    $factory '';
          
    $fdepartment '';
          
    $designation '';
          
    $mrchand_name ''

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mehidy View Post
    Dear

    Thanks for your response. Kindly show me an example, how can I change the code.
    For example using the ternary operator (see example #2):
    PHP Code:
    $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:
    PHP Code:
          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.

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much,,,

    its working fine.

    Quote Originally Posted by guido2004 View Post
    For example using the ternary operator (see example #2):
    PHP Code:
    $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:
    PHP Code:
          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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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" />

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mehidy View Post
    Sorry..now facing new problem as below

    Undefined variable: error in D:\xampp\htdocs\xampp\mehidy\ano5.php on line
    You don't initialize $error anywhere, so if there's no errors, the variable $error doesn't exist.
    Quote Originally Posted by mehidy View Post
    Undefined index at line this
    This is the same problem as before. Check if the $_POST variable exists before using it.

  9. #9
    SitePoint Member
    Join Date
    Nov 2011
    Location
    chennai
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    $a['c'];

    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...#ixzz1d77H58bn
    Last edited by guido2004; Nov 8, 2011 at 05:52. Reason: deleted fake sig


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •