下面
有一个mysqli/php代码,它将根据从question
下拉菜单中选择的选项显示结果:
$selectedquestionqry = "
SELECT
QuestionNo
FROM
Question
WHERE
(QuestionId = ?)
";
global $mysqli;
$selectedquestionstmt=$mysqli->prepare($selectedquestionqry);
// You only need to call bind_param once
$selectedquestionstmt->bind_param("i",$_POST["question"]);
// get result and assign variables (prefix with db)
$selectedquestionstmt->execute();
$selectedquestionstmt->bind_result($selQuestionNo);
$selectedquestionstmt->store_result();
$selquestionnum = $selectedquestionstmt->num_rows();
while ($selectedquestionstmt->fetch()) {
if($_POST["question"] === '0') {
echo "<p>All Questions - Total:(" . $selquestionnum . ")</p>" . PHP_EOL;
}else if($_POST["question"] !== '0') {
echo "<p><strong>Questions: </strong>" . $selQuestionNo . "</p>" . PHP_EOL;
}
}
下拉菜单:
<select name="student" id="studentsDrop">
<option value="0">All</option>
<option value="23">Jay Hart</option>
<option value="32">Bubba Wright</option>
</select>
我的问题是,我如何获取它,以便如果用户选择了"0",那么它将能够从数据库中选择显示在question
下拉菜单中的所有问题?
我问这个问题的原因是,在我的回声else if($_POST["question"] !== '0') {
echo "<p><strong>Questions: </strong>" . $selQuestionNo . "</p>" . PHP_EOL;
}
中,当我选择All
选项时,没有任何回声,这对我来说让我认为它没有因此而显示回声。如果我从下拉菜单中选择一个问题,它能够输出它的回声。
你只需要修改你的查询:
if($_POST["question"] === '0') {
$selectedquestionqry = "SELECT QuestionNo FROM Question";
} else {
$selectedquestionqry = "SELECT QuestionNo FROM Question WHERE (QuestionId = ?)";
}
您需要
更改查询以删除基于正在'0'
的已发布值的WHERE
条件。 之后,您不必更改任何代码,因为您已经在循环,但您应该在循环之外显示 Total。