使用数据库中的数据进行多项选择


making multiple choice with data from database

我想做多项选择,问题来自数据库。我确信我的数据库名称是正确的,但当我点击下一个时,问题不会改变,它直接进入问题的末尾/结果,问题也不是随机的。我插入数据库的最后一个问题显示在网页中。请帮助

这是我的代码:

<?php
require_once('includes/db_conn.php');
$query = "select * from question";
$query_result = $dbc->query($query);
$num_questions_returned = $query_result->num_rows;
if ($num_questions_returned < 1){
    echo "There is no question in the database";
    exit();}
$questionsArray = array();
while ($row = $query_result->fetch_assoc()){
    $questionsArray[] = $row;
}
$correctAnswerArray = array();
foreach($questionsArray as  $question){
    $correctAnswerArray[$question['question']] = $question['correct_answer'];
}
$questions = array();
foreach($questionsArray as $question) {
    $questions[$question['question']] = $question['question'];
 }
$choices = array();
foreach ($questionsArray as $row) {
    $choices[$row['question']] = array($row['wrong_answer1'], $row['wrong_answer2'], $row['wrong_answer3'], $row['correct_answer']);
  }
error_reporting(0);
$address = "";
$randomizequestions ="yes";
$a = array(
1 => array(
   0 => $question['question'],
   1 => $row['wrong_answer1'],
   2 => $row['wrong_answer2'],
   3 => $row['wrong_answer3'],
   4 => $row['correct_answer'],
   6 => 4
),
);
$max=1;
$question=$_POST["question"] ;
if ($_POST["Randon"]==0){
        if($randomizequestions =="yes"){$randval = mt_rand(1,$max);}else{$randval=1;}
        $randval2 = $randval;
        }else{
        $randval=$_POST["Randon"];
        $randval2=$_POST["Randon"] + $question;
                if ($randval2>$max){
                $randval2=$randval2-$max;
                }
        }
$ok=$_POST["ok"] ;
if ($question==0){
        $question=0;
        $ok=0;
        $percentage=0;
        }else{
        $percentage= Round(100*$ok / $question);
        }
?>
<HTML><HEAD>
<SCRIPT LANGUAGE='JavaScript'>
<!-- 
function Goahead (number){
        if (document.percentaje.response.value==0){
                if (number==<?php print $a[$randval2][6] ; ?>){
                        document.percentaje.response.value=1
                        document.percentaje.question.value++
                        document.percentaje.ok.value++
                }else{
                        document.percentaje.response.value=1
                        document.percentaje.question.value++
                }
        }
        if (number==<?php print $a[$randval2][6] ; ?>){
                document.question.response.value="Correct"
        }else{
                document.question.response.value="Incorrect"
        }
}
// -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=FFFFFF>
<CENTER>
<H1><?php print "$title"; ?></H1>
<TABLE BORDER=0 CELLSPACING=5 WIDTH=500>
<?php if ($question<$max){ ?>
<TR><TD ALIGN=RIGHT>
<FORM METHOD=POST NAME="percentaje" ACTION="<?php print $URL; ?>">
<BR>Percentaje of correct responses: <?php print $percentage; ?> %
<BR><input type=submit value="Next >>">
<input type=hidden name=response value=0>
<input type=hidden name=question value=<?php print $question; ?>>
<input type=hidden name=ok value=<?php print $ok; ?>>
<input type=hidden name=Randon value=<?php print $randval; ?>>
<br><?php print $question+1; ?> / <?php print $max; ?>
</FORM>
<HR>
</TD></TR>
<TR><TD>
<FORM METHOD=POST NAME="question" ACTION="">
<?php print "<b>".$a[$randval2][0]."</b>"; ?>
<BR>     <INPUT TYPE=radio NAME="option" VALUE="1"  onClick=" Goahead (1);"><?php print $a[$randval2][1] ; ?>
<BR>     <INPUT TYPE=radio NAME="option" VALUE="2"  onClick=" Goahead (2);"><?php print $a[$randval2][2] ; ?>
<?php if ($a[$randval2][3]!=""){ ?>
<BR>     <INPUT TYPE=radio NAME="option" VALUE="3"  onClick=" Goahead (3);"><?php print $a[$randval2][3] ; } ?>
<?php if ($a[$randval2][4]!=""){ ?>
<BR>     <INPUT TYPE=radio NAME="option" VALUE="4"  onClick=" Goahead (4);"><?php print $a[$randval2][4] ; } ?>
<BR>     <input type=text name=response size=8>

</FORM>
<?php
}else{
?>
<TR><TD ALIGN=Center>
The Quiz has finished
<BR>Percentage of correct responses: <?php print $percentage ; ?> %
<p><A HREF="<?php print $address; ?>">Home Page</a>
<?php } ?>
</TD></TR>
</TABLE>
</CENTER>
</BODY>
</HTML>

这是我将数据添加到数据库的过程:

<?php
include('includes/header.html');
error_reporting(-1);
ini_set('display_errors', 'On');
//Check for empty fields
if(empty($_POST['question'])||
    empty($_POST['correct_answer']) ||
    empty($_POST['wrong_answer1'])      ||
    empty($_POST['wrong_answer2'])      ||
    empty($_POST['wrong_answer3']))
{
    echo "Please complete all fields";
    exit();
}
//Create short variables
$question = $_POST['question'];
$correct_answer = ($_POST['correct_answer']);
$wrong_answer1 = ($_POST['wrong_answer1']);
$wrong_answer2 = ($_POST['wrong_answer2']);
$wrong_answer3 = ($_POST['wrong_answer3']);
//connect to the database
require_once('includes/db_conn.php');
//Create the insert query
$query = "INSERT INTO question VALUES ('$question', '$correct_answer', '$wrong_answer1','$wrong_answer2','$wrong_answer3')";
$result = $dbc->query($query);
if($result){
    echo "Your quiz has been saved";
} else {
    echo '<h1>System Error</h1>';
}
$dbc->close();
?>

您的第一个错误是生成$a数组。为什么不使用上次for循环中生成的$choices?对于随机问题,请处理$choice数组,而next的提交按钮在表单之外,因此您将无法在next按钮上获得表单值。请将其放入表单标记中。

另一件事是在单选按钮上点击不写任何东西,只需在提交点击事件上写下您的函数,然后在保存上一个答案后处理下一个问题。

这些是我的建议。。要获得更多帮助,请将您的数据库及其连接的文件放在这里备份,以便我可以帮助您进行编码。