0

我在下面收到这些错误:

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

这些错误是什么意思,如何解决?我看不出问题出在哪里,但我知道在单击“模块提交”按钮后它们会出现

$模块活动 = 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);

}

?>
4

2 回答 2

1

发生此错误是因为 $moduleInfo 变量中该索引处没有值。由于没有进入 $_POST['modules'] 变量

替换它以避免错误

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

$moduleName = isset($moduleInfo[1])?$moduleInfo[1]:''; 
$moduleId = isset($moduleInfo[2])?$moduleInfo[2]:'';
于 2013-02-15T07:47:03.550 回答
0

该错误意味着未定义1和引用的元素。2

您正在explode输入一个带有下划线的字符串,正如用户在modules. 如果该字符串没有任何(或足够的)下划线,explode则返回一个元素比您想象的要少的数组。

您应该在使用之前验证输入,并检查您是否实际上至少有 3 个元素:

$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
}
于 2013-02-15T07:51:43.820 回答