提供了未定义的变量和无效的参数
如果我从下拉菜单中选择一个学生(如果$_POST['student'] != 0
值为0
代表All
选项,并且所有其他选项都有自己的数字),那么这很奇怪ubt,我得到一个错误,说明:提供了未定义的变量和无效的参数
Notice: Undefined variable: questions in ... on line 597 Warning: Invalid argument supplied for foreach() in ... on line 597
我的问题是什么原因造成这个错误,怎么能解决吗?
代码如下,我曾试图降低尽可能靠近代码片段的底部就是错误行是被注释掉:
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname
...
FROM Student st
...
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
//check if POST is empty
// Check whether a specific student was selected
$p_student = empty($_POST["student"])?0:$_POST["student"]; // Now if $_POST['student'] is either 0 or empty $p_student will be 0
switch($p_student){
case 0:
//dont' add where filters
break;
default:
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
// Check whether a specific question was selected
$p_question = empty($_POST["question"])?0:$_POST["question"]; // Same here, if $_POST['question'] is either 0 or empty $p_question will be 0
switch($p_question){
case 0:
//dont' add where filters
break;
default:
$where[] = 'q.QuestionId = ?';
$parameters[] .= $_POST["question"];
$parameterTypes .= 'i';
}
// If we added to $where in any of the conditionals, we need a WHERE clause in
// our query
if(!empty($where)) {
$selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
// You only need to call bind_param once
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
if (count($where) == 1) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
}
else if (count($where) == 2) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
}
else if (count($where) == 3) {
$selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1], $parameters[2]);
}
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();
$arrStudentId = array();
$arrStudentAlias = array();
...
while ($selectedstudentanswerstmt->fetch()) {
//Check if the student data exist.
if (!isset($questions[$detailsStudentId])) {
$questions[$detailsStudentId] = array(
'studentalias' => $detailsStudentAlias,
'studentforename' => $detailsStudentForename,
'studentsurname' => $detailsStudentSurname,
'questions' => array()
);
}
$questions[$detailsStudentId]['questions'][$detailsQuestionId] = array(
'questionno'=>$detailsQuestionNo,
'content'=>$detailsQuestionContent,
.....
);
}
$selectedstudentanswerstmt->close();
?>
//LINE 597 ERROR BELOW
foreach ($questions as $studentId => $studentData) {
echo '<hr><h3>'.$studentData['studentalias'].' - '.$studentData['studentforename'].' '.$studentData['studentsurname'].'</h3>';
foreach ($studentData['questions'] as $questionId => $questionData) {
echo '<h3>'.$questionData['questionno'].': '.$questionData['content'].'</h3>';
}
}
的变量$问题本身可能是初始化问题。仔细查看你的代码,看看$questions =
被调用的地方。可能每次都是在没有运行的情况下发言。
如果您拨打:
$questions[...]
,从不初始化$questions
作为数组你也会有问题。 所以,你需要初始化$questions
:在此之后
$questions = array();
你不会有任何问题,访问数组的领域。
'$ questions [...]''我宁愿说“也可能有问题” PHP它按预期工作,在一些意外的行为 – Kyborek 2013-02-19 17:30:41
是的,当然,但这就是为什么你应该初始化它;)但感谢提示! – Benny 2013-02-19 17:34:44
@Benny wheredo我放置'$ questions = array();'因为上面while循环它不输出任何数据,while循环在第一个if语句之上,它仍然显示错误 – user2048994 2013-02-19 17:37:26
由于您刚刚发布了部分源代码,因此该定义可能位于if语句或永不执行的循环中。寻找那 – Benny 2013-02-19 17:16:03
@Benny这就是问题所在,我想是在寻找一个挎包还是冒号,或者我想要找什么?我的代码片段中的'...'就像一个长的代码列表等,它不包含任何括号或类似的东西,只是让代码看起来更小,如果你知道我的意思。您看到的结构与完整代码 – user2048994 2013-02-19 17:24:27
的结构完全相同。例如,查看所有'$ questions'的出现,或者至少将它们放在这里,因为它的重要部分在您的示例中可以跳过。或者检查'while($ selectedstudentanswerstmt-> fetch())'是否至少循环了一次,否则问题可能是空的 – Kyborek 2013-02-19 17:28:20