0

下面有一个 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下拉菜单中显示的数据库中选择所有问题?

我问这个的原因是因为在我的 echoelse if($_POST["question"] !== '0') { echo "<p><strong>Questions: </strong>" . $selQuestionNo . "</p>" . PHP_EOL; }中,当我选择该All选项时,什么都没有回显,这让我认为它没有因此显示回声。如果我从下拉菜单中选择一个问题,它能够输出它的回声。

4

2 回答 2

2

你只需要修改你的查询:

if($_POST["question"] === '0') {
    $selectedquestionqry = "SELECT QuestionNo FROM Question";
} else {
    $selectedquestionqry = "SELECT QuestionNo FROM Question WHERE (QuestionId = ?)";
}
于 2013-02-03T18:25:35.123 回答
0

您需要更改查询以删除WHERE基于发布值的条件 '0'。此后您不必更改任何代码,因为您已经在循环,但您应该在循环之外显示 Total。

于 2013-02-03T18:23:06.070 回答