我收到以下错误:
Notice: Undefined offset: 1 in ... on line 93
Notice: Undefined offset: 2 in ... on line 94
这些错误意味着什么以及如何解决?我无法看到问题所在,但我知道它们在点击“模块提交”按钮后出现
$ 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);
}
?>
答案 0 :(得分:1)
发生此错误,因为$ moduleInfo varibale中的索引没有值。 因为没有进入$ _POST ['modules'] varible
替换它以避免错误
$moduleName =$moduleInfo[1]; //Error line 93
$moduleId =$moduleInfo[2]; //Error line 94
与
$moduleName = isset($moduleInfo[1])?$moduleInfo[1]:'';
$moduleId = isset($moduleInfo[2])?$moduleInfo[2]:'';
答案 1 :(得分:0)
错误表示未定义1
和2
引用的元素。
您explode
用modules
用户发布的带有下划线的字符串explode
。如果该字符串没有任何(或足够的)下划线,$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
}
将返回一个元素少于您想象的数组。
您应该在使用之前验证输入,并检查您是否确实至少有3个元素:
{{1}}