0

I am receiving these errors below:

Notice: Undefined offset: 1 in ... on line 93 
Notice: Undefined offset: 2 in ... on line 94 

What does these errors mean and how can it be fixed? I can't see where the problem is but I know they are appearing after clicking on the "Module Submit" button

$moduleactive = 1;

    $sql = "SELECT ModuleId, ModuleNo, ModuleName FROM Module WHERE ModuleActive = ? ORDER BY ModuleNo"; 


     $sqlstmt=$mysqli->prepare($sql);

     $sqlstmt->bind_param("i", $moduleactive);

     $sqlstmt->execute(); 

     $sqlstmt->bind_result($dbModuleId,$dbModuleNo,$dbModuleName);


    $moduleHTML  = "";  
      $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
 $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

     while($sqlstmt->fetch()) { 
         $moduleHTML .= sprintf('<option value="%1$s_%2$s_%3$s">%1$s - %2$s</option>'.PHP_EOL, $dbModuleNo, $dbModuleName, $dbModuleId);
    } 

$moduleHTML .= '</select>'; 

    $pHTML = "";


    ?>


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation(event);">
<table>
<tr>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" /></p>
<div id="moduleAlert"></div>
<div id="targetdiv"></div>
</form>

<?php

if (isset($_POST['moduleSubmit'])) {    

$outputmodule = ""; 

$moduleInfo = explode("_", $_POST['modules']);
$moduleNo = $moduleInfo[0];
$moduleName = $moduleInfo[1]; //Error line 93
$moduleId = $moduleInfo[2]; //Error line 94
$outputmodule = sprintf("<p><strong>Selected Module:</strong> %s - %s <input type='hidden' value='%s'></p>", $moduleNo, $moduleName, $moduleId);

}

?>
user2048994
  • 250
  • 3
  • 13
  • 1
    `$_POST['modules']` doesn't have enough `_` separated values. – SparKot Feb 15 '13 at 07:47
  • 1
    [You agreed to do thorough research](http://stackoverflow.com/questions/ask-advice) before you could ask this question. How come you did not find the answer in the [7,65 million search results for the Notice](https://www.google.de/search?q=undefined+offset+1+and+2+errors) nor in the [dozens of existing questions on StackOverflow](http://stackoverflow.com/search?q=Notice+undefined+offset+1+and+2+errors+php) – Gordon Feb 15 '13 at 08:16
  • 1
    Better check nice [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) first, e.g. this might match: http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12770836#12770836 – hakre Feb 15 '13 at 08:16

2 Answers2

1

This error occure because there is no value at that index in $moduleInfo varibale. As not coming in $_POST['modules'] varible

Replace this to avoid error

$moduleName =$moduleInfo[1]; //Error line 93
$moduleId =$moduleInfo[2]; //Error line 94

with

$moduleName = isset($moduleInfo[1])?$moduleInfo[1]:''; 
$moduleId = isset($moduleInfo[2])?$moduleInfo[2]:'';
sandip
  • 3,161
  • 5
  • 27
  • 52
0

The error means that the elements referenced by 1 and 2 are not defined.

You are explodeing a string with underscores as posted by the user in modules. If that string does not have any (or enough) underscores, explode comes back with an array with less elements than you think.

You should validate the input before you use it, and check to see if you actually have at least 3 elements:

$moduleInfo = explode("_", $_POST['modules']);
if (count($moduleInfo) >= 3) {
   $moduleNo = $moduleInfo[0];
   $moduleName = $moduleInfo[1]; //Error line 93
   $moduleId = $moduleInfo[2]; //Error line 94

   // validate the subparts (this is important as well!)
} else {
   // set your variables to some sane default or fail gracefully
}
Bart Friederichs
  • 30,888
  • 13
  • 85
  • 169