4

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

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

警告:mysqli_stmt::execute(): (HY000/2031): 没有为第 327 行的准备好的语句中的参数提供数据

警告:mysqli_stmt::bind_result(): (HY000/2031): No data provided for parameters in Prepared statement in ... on line 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="answerSubmit" type="submit" value="Get Student's Answers" name="answerSubmit" />
</form>

<?php
}
}

function StudentAnswersIsSubmitted()
{

if(!isset($_POST["answerSubmit"]))
    {
        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";

}

?>
4

1 回答 1

-2

看看fluentPDO、 Dibi fluent 或一些类似的查询生成器。它将使您免于许多痛苦,并且他们已经解决了您正在尝试做的事情。

无论如何,而不是:

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

利用:

call_user_func_array(array($selectedstudentanswerstmt, 'bind_param'),
    array_merge(array($parameterTypes), $parameters))
于 2013-01-30T02:08:55.923 回答