我正在尝试遵循对答案的参考,但不确定应该如何编写:
empty() 将 '0' 视为空,因此在 $p_student = empty($_POST["student"])?'':$_POST["student"]; 当 $_POST["student"] 为 '0' 时,$p_student 为真...因此,以下情况始终为 'default',因此如果为 empty() 并且您应该将 $p_student 设置为 '0'应该没问题...我认为...(当然,这适用于 $p_student 和 $p_question...)
我已经设置了一个案例语句来匹配上面的例子,但我的问题是应该如何根据上面的例子编写 $p_student ?
我的尝试:
// Check whether a specific student was selected
$p_student = empty($_POST["student"])?'0':$_POST["student"];
switch($p_student){
case -1:
//dont' add where filters
break;
default:
$where[] = 'sa.StudentId = ?';
$parameters[] .= $_POST["student"];
$parameterTypes .= 'i';
}
更新:
我试图做的是如果All
选项,然后执行案例-1,否则执行默认值。我做错了什么吗,因为这就是为什么我在下拉值上提出这个问题:
学生:
<select name="student" id="studentsDrop">
<option value="-1">All</option>
<option value="39">Luke Mcfadzen</option>
<option value="40">Chris Tucker</option>
</select>
问题:
<select name="question" id="questionsDrop">
<option value="-1">All</option>
<option value="72">1</option>
<option value="73">2</option>
</select>
动态 where 子句是我正在尝试的,这取决于用户是选择所有学生还是单个学生以及所有问题或单个问题。如果个人则使用 where 子句查找学生,并且问题相同,如果所有学生则不需要 where 条件学生,因为我们不是在寻找特定的学生,所以这对问题的工作方式相同。该查询具有强制性的 WHERE 条件检查q.SessionId = ?
我收到错误说明:
$selectedstudentanswerqry = "
SELECT
sa.StudentId, StudentAlias, StudentForename, ...
FROM Student st
...
";
// Initially empty
$where[] = "q.SessionId = ?";
$parameters[] = $_POST["session"];
$parameterTypes = 'i';
//check if POST is empty
// Check whether a specific student was selected
//LINE 345 ERROR
$student_id = (isset($_POST['student'])) ? $mysqli->real_escape_string(trim($_POST['student'])) : null ;
if (is_numeric($student_id)){ //If student ID is a numeric value
$where[] = "sa.StudentId = ?" ;
$parameters[] = ((int)$student_id == -1) ? "sa.StudentId" : $student_id ;
$parameterTypes .= "i" ;
}
// Check whether a specific question was selected
$question_id = (isset($_POST['question'])) ? $mysqli->real_escape_string(trim($_POST['question'])) : null ;
if (is_numeric($question_id)){ //If student ID is a numeric value
$where[] = "q.QuestionId = ?" ;
$parameters[] = ((int)$question_id == -1) ? "q.QuestionId" : $question_id ;
$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);
global $mysqli;
$selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
// You only need to call bind_param once
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]);
}
}
$selectedstudentanswerqry .= "
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId, QuestionNo
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute();
$selectedstudentanswerstmt->bind_result(...);
$selectedstudentanswerstmt->store_result();
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();