创建动态 where 子句时出错


getting errors when creating a dynamic where clause

尝试使用 mysqli 创建动态 where 子句时,我遇到了很多错误:

警告:mysqli_stmt::bind_param():类型中的元素数 定义字符串与 ... 中的绑定变量数不匹配...上 318路

警告:mysqli_stmt::执行():(HY000/2031):未提供 的数据 准备语句中的参数...在线 327

警告:mysqli_stmt::bind_result():(HY000/2031):未提供数据 对于准备语句中的参数...330行

警告:mysqli_stmt::store_result():(HY000/2014):命令不在 同步;您现在无法在...在第 331 行

我猜需要一些改变来解决问题,但发生的事情是,如果两个下拉菜单中的一个不等于All或者如果两个都不等于All那么它就会出现错误。

下面是代码显示下拉菜单和查询(带有动态 where 子句),具体取决于所选的 n 个选项:

   function ShowAssessment()
{   
$studentactive = 1;
$currentstudentqry = "
SELECT
st.StudentId, st.StudentAlias, st.StudentForename, st.StudentSurname
FROM
Student_Session ss 
INNER JOIN
Student st ON ss.StudentId = st.StudentId
WHERE
(ss.SessionId = ? and st.Active = ?)
ORDER BY st.StudentAlias
";
global $mysqli;
$currentstudentstmt=$mysqli->prepare($currentstudentqry);
// You only need to call bind_param once
$currentstudentstmt->bind_param("ii",$_POST["session"], $studentactive);
// get result and assign variables (prefix with db)
$currentstudentstmt->execute(); 
$currentstudentstmt->bind_result($dbStudentId,$dbStudentAlias,$dbStudentForename,$dbStudentSurname);
$currentstudentstmt->store_result();
$studentnum = $currentstudentstmt->num_rows();       
if($studentnum == 0){ ?>
<div class="red">
There are no Students who have currently taken this Assessment
</div>
<?php } else { 
$questionsqry = "
SELECT
QuestionId, QuestionNo
FROM
Question
WHERE
(SessionId = ?)
ORDER BY QuestionNo
";
global $mysqli;
$questionsstmt=$mysqli->prepare($questionsqry);
// You only need to call bind_param once
$questionsstmt->bind_param("i",$_POST["session"]);
// get result and assign variables (prefix with db)
$questionsstmt->execute(); 
$questionsstmt->bind_result($dbQuestionId,$dbQuestionNo);
$questionsstmt->store_result();
$studentnum = $questionsstmt->num_rows();      
        ?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">         
<p>
 <input type="hidden" name="module" value="<?php echo $_POST['module']; ?>">
<input type="hidden" name="session" value="<?php echo $_POST['session']; ?>">
<strong>Student:</strong>
<select name="student" id="studentsDrop">
<option value="All">All</option>
<?php
while ( $currentstudentstmt->fetch() ) {
$stu = $dbStudentId;
if(isset($_POST["student"]) && $stu == $_POST["student"]) 
    echo "<option selected='selected' value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
else
    echo "<option value='$stu'>" . $dbStudentAlias . " - " . $dbStudentForename . " " . $dbStudentSurname . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<p>
<strong>Question:</strong>
<select name="question" id="questionsDrop">
<option value="All">All</option>
<?php
while ( $questionsstmt->fetch() ) {
$ques = $dbQuestionId;
if(isset($_POST["question"]) && $ques == $_POST["question"]) 
    echo "<option selected='selected' value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
else
    echo "<option value='$ques'>" . $dbQuestionNo . "</option>" . PHP_EOL;
}
?>
</select>
</p>
<input id="answerswerSubmit" type="submit" value="Get Student's Answers" name="answerswerSubmit" />
</form>
<?php
}
}
function StudentAnswersIsSubmitted()
{
if(!isset($_POST["answerswerSubmit"]))
    {
        return false;
    }
    else // All is ok
    {
        return true;
    }
    return false;
}
function StudentAnswers()
{
$selectedstudentanswerqry = "
SELECT
StudentAlias, StudentForename, StudentSurname, q.SessionId, QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, GROUP_CONCAT( DISTINCT Answer
ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
FROM Student s
INNER JOIN Student_Answer sa ON (s.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
";
// Initially empty
$where = array('q.SessionId = ?');
$parameters = array($_POST["session"]);
$parameterTypes = 'i';
// Check whether a specific student was selected
if($_POST["student"] !== 'All') {
    $where[] = 'sa.StudentId = ?';
    $parameters[] = $_POST["student"];
    $parameterTypes .= 'i';
}
// Check whether a specific question was selected
// NB: This is not an else if!
if($_POST["question"] !== 'All') {
    $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);
    global $mysqli;
    $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
    // You only need to call bind_param once
    $selectedstudentanswerstmt->bind_param($parameterTypes,implode($parameters));  //LINE 318
}
$selectedstudentanswerqry .= "
  GROUP BY sa.StudentId, q.QuestionId
  ORDER BY StudentAlias, q.SessionId, QuestionNo
";
// get result and assign variables (prefix with db)
$selectedstudentanswerstmt->execute(); //LINE 327
$selectedstudentanswerstmt->bind_result($detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo, 
$detailsQuestonContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
$detailsMouseClick,$detailsStudentMark); //LINE 330
$selectedstudentanswerstmt->store_result(); //LINE 331
$selectedstudentanswernum = $selectedstudentanswerstmt->num_rows();     
echo "$selectedstudentanswerqry";
}
?>

看看fluentPDO,Dibi fluent或一些类似的查询生成器。它将使您免于很多痛苦,他们已经解决了您要做的事情。

无论如何,而不是:

$selectedstudentanswerstmt->bind_param($parameterTypes,implode($parameters));

用:

call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'),
    array_merge(array($parameterTypes), $parameters))