0

questionドロップダウン メニューから選択したオプションに応じて結果を表示する mysqli/php コードを以下に示します。

$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>

question私の質問は、ユーザーが「0」を選択した場合、ドロップダウンメニューに表示されるデータベースからすべての質問を選択できるようにするにはどうすればよいですか?

これを尋ねている理由は、私の echoelse if($_POST["question"] !== '0') { echo "<p><strong>Questions: </strong>" . $selQuestionNo . "</p>" . PHP_EOL; }では、オプションを選択しても何もエコーさAllれていないためです。これにより、エコーが表示されていないと思います。ドロップダウン メニューから 1 つの質問を選択すると、エコーを出力できます。

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 に答える