我正在运行这段代码。它显示一个问题和可能的答案。当我单击提交按钮时。转到空白页。
我收到错误"未定义的索引计数"和"未定义的索引主题"。因此,这告诉我它再次运行查询,但没有设置索引的值。
如何获得此提交按钮,以仅显示查询所选数据的数据列" rightAnswer",而不是再次运行查询?
define('DB_NAME', 'quizquestions');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
# connects to the database$link = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD);
# Provides feedback that the connection is madeif (!$link) {
die ('could not connect:' . mysqli_error());
}
#connects tot he database$db_selected = mysqli_select_db($link, DB_NAME);
if (!$db_selected) {
die('Can\'t use') . DB_NAME;
}
$limitValue = 0;
$type = 'myTopic';
if(isset($_POST['count'])){
$limitValue = $_POST['count'];
}
if(isset($_POST['topic'])){
$type = $_POST['topic'];
}
$qType = join("','", $type);
if(isset($_POST['submit'])){
// create a list of SQL"OR"s to select only those questions that were answered rather than randomly.
$questionList ="`question_id`=".$_POST['Question'][0];
for($i = 1; $i < $_POST['Question']; $i++){
$questionList .=" OR `question_id`=".$_POST['Question'][$i];
}
$sql ="SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE $questionList";
} else {
// randomly select as many questions as specified
$sql ="SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE `questionType` IN ('$qType') ORDER BY RAND() LIMIT $limitValue";
}
$result = mysqli_query($link, $sql);
$num = mysqli_num_rows($result);
// output data from each row
while ($row = mysqli_fetch_array($result)) {
$qID = $row->question_id;
$a = $row->answerA;
$b = $row->answerB;
$c = $row->answerC;
$d = $row->answerD;
$rA = $row->rightAnswer;
echo 'QID: '.$qID.''.
'Question Type: '.$row->question_type.'' .
'Question: '.$row->question.'';
echo ' A. '.$a.' ';
echo ' B. '.$b.' ';
echo ' C. '.$c.' ';
echo ' D. '.$d.' ';
echo '';
if(isset($_POST['submit'])){
if(isset($_POST['Answer'][$qID])){
if($_POST['Answer'][$qID] == $rA){
echo '
Correct!
} else {
echo '
Wrong!
}
echo 'Right Answer: '.$rA.'';
} else {
echo '
No answer!
}
echo '';
}
}
echo '';
mysqli_free_result($result);
mysqli_close($link);
?>
从error_log添加最新条目:
[2018年8月26日21:25:43 UTC] PHP注意:未定义的索引:在第2行的/home/myd2n7jhklvu/public_html/quiz.php中计数
[2018年8月26日21:25:43 UTC] PHP注意:未定义的索引:第3行/home/myd2n7jhklvu/public_html/quiz.php中的主题
[2018年8月26日21:25:43 UTC] PHP警告:join():在第30行的/home/myd2n7jhklvu/public_html/quiz.php中传递了无效的参数
[2018年8月26日21:25:46 UTC] PHP警告:join():在第30行的/home/myd2n7jhklvu/public_html/quiz.php中传递了无效的参数
[2018年8月26日21:25:46 UTC] PHP注意:未定义的变量:第54行/home/myd2n7jhklvu/public_html/quiz.php中的qID
[26-Aug-2018 21:25:47 UTC] PHP注意:在第3行的/home/myd2n7jhklvu/public_html/quiz.php中将数组转换为字符串
[2018年8月26日21:26:04 UTC] PHP警告:join():在第30行的/home/myd2n7jhklvu/public_html/quiz.php中传递了无效的参数
[2018年8月26日21:26:04 UTC] PHP注意:未定义的变量:第54行的/home/myd2n7jhklvu/public_html/quiz.php中的qID
您的提交按钮是什么样的?
我在原始评论中添加了查询结果的摘要。
抱歉,没有看到您提交按钮的代码行。 那就是我想要的。
我看到您是新的贡献者,所以欢迎您!
我相信这些索引是未定义的,因为您没有像先检查$_POST['submit']那样检查它们是否首先存在。
因此,我将在您的代码开头执行此操作:
$limitValue = 0; // or any other default value
$type = 'myTopic'; // or any other default value
if(isset($_POST['count'])){
$limitValue = $_POST['count']; // overwrite the default value
}
if(isset($_POST['topic'])){
$type = $_POST['topic']; // overwrite the default value
}
无论如何,我都不知道您希望页面的行为如何,但是我想最好只有一个"提交"按钮来将其全部发送并一起打印。
细节
因此,看一下代码,嵌套的while循环我错了! 缩进有点差,所以我没有看到闭合的花括号。
无论如何,我重写了代码,并且我相信这应该可行:
$limitValue = 0; // or any other default value
$type = ['myTopic']; // or any other default value
if(isset($_POST['count'])){
$limitValue = $_POST['count']; // overwrite the default value
}
if(isset($_POST['topic'])){
$type = $_POST['topic']; // overwrite the default value
}
$qType = join("','", $type);
if(isset($_POST['submit'])){
// create a list of SQL"OR"s to select only those questions that were answered rather than randomly.
$questionList ="`question_id`=".$_POST['Question'][0];
for($i = 1; $i < count($_POST['Question']); $i++){
$questionList .=" OR `question_id`=".$_POST['Question'][$i];
}
$sql ="SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE $questionList";
} else {
// randomly select as many questions as specified
$sql ="SELECT `question_id`, `questionType`, `question`, `answerA`, `answerB`, `answerC`, `answerD`, `rightAnswer` FROM `questions` WHERE `questionType` IN ('$qType') ORDER BY RAND() LIMIT $limitValue";
}
$result = $link->query($sql);
if($result->num_rows > 0){
// output data from each row
echo '';
echo '';
while($row = $result->fetch_object()){
$qID = $row->question_id;
$a = $row->answerA;
$b = $row->answerB;
$c = $row->answerC;
$d = $row->answerD;
$rA = $row->rightAnswer;
echo 'QID: '.$qID.''.
'Question Type: '.$row->questionType.'' .
'Question: '.$row->question.'';
echo ' A. '.$a.' ';
echo ' B. '.$b.' ';
echo ' C. '.$c.' ';
echo ' D. '.$d.' ';
echo '';
if(isset($_POST['submit'])){
if(isset($_POST['Answer'][$qID])){
if($_POST['Answer'][$qID] == $rA){
echo '
Correct!
} else {
echo '
Wrong!
}
echo 'Right Answer: '.$rA.'';
} else {
echo '
No answer!
}
}
}
echo '';
echo '';
}
$link->close();
?>
您可以尝试将其另存为临时性Cookie和/或将其存储到本地存储。
我不知道从哪里开始,但我会研究它。