Php 如果用户选择“全部”,如何从所有选项检索数据`

Php 如果用户选择“全部”,如何从所有选项检索数据`,php,mysql,mysqli,Php,Mysql,Mysqli,下面有一个mysqli/php代码,它将根据从question下拉菜单中选择的选项显示结果: $selectedquestionqry = " SELECT QuestionNo FROM Question WHERE (QuestionId = ?) "; global $mysqli; $selectedquestionstmt=$mysqli->prepare($selectedquestionqry); // You only need to call bind_param on

下面有一个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;
}
}
$selectedquestionqry=”
挑选
问题不
从…起
问题:
哪里
(问题ID=?)
";
全球$mysqli;
$selectedquestionstmt=$mysqli->prepare($selectedquestionqry);
//您只需要调用bind_param一次
$selectedquestionstmt->bind_-param(“i”,“$”POST[“问题]);
//获取结果并分配变量(前缀为db)
$selectedquestionstmt->execute();
$selectedquestionstmt->bind_result($selsquestionno);
$selectedquestionstmt->store_result();
$selquestionnum=$selectedquestionstmt->num_rows();
while($selectedquestionstmt->fetch()){
如果($_POST[“问题”]=“0”){
echo“所有问题-总计:(“$selquestionnum”)

”.PHP\u EOL; }else if($_POST[“问题”]!='0'){ echo“问题:”$selQuestionNo.

”.PHP\u 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
下拉菜单中的问题

我问这个问题的原因是因为在我的echo
else if($\u POST[“question”]!==“0”){
echo“问题:”$selQuestionNo.

”.PHP\u EOL;
}
,当我选择
All
选项时,没有显示任何回显,这使我认为它没有显示回显。如果我从下拉菜单中选择一个问题,它就能够输出它的回音。

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

您只需修改查询:

if($_POST["question"] === '0') {
    $selectedquestionqry = "SELECT QuestionNo FROM Question";
} else {
    $selectedquestionqry = "SELECT QuestionNo FROM Question WHERE (QuestionId = ?)";
}

当问题===0时,使用这些条件处理查询,并删除查询中的remove where子句