gpt4 book ai didi

php - 创建动态 where 子句时出错

转载 作者:可可西里 更新时间:2023-10-31 23:51:15 25 4
gpt4 key购买 nike

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

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in ... on line 318

Warning: mysqli_stmt::execute(): (HY000/2031): No data supplied for parameters in prepared statement in ... on line 327

Warning: mysqli_stmt::bind_result(): (HY000/2031): No data supplied for parameters in prepared statement in ... on line 330

Warning: mysqli_stmt::store_result(): (HY000/2014): Commands out of sync; you can't run this command now in ... on line 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";

}

?>

最佳答案

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

无论如何,而不是:

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

使用:

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

关于php - 创建动态 where 子句时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com