Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 利用数据库中的数据进行多项选择_Javascript_Php_Html - Fatal编程技术网

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

Javascript 利用数据库中的数据进行多项选择,javascript,php,html,Javascript,Php,Html,我想做一个选择题,问题来自数据库。我确信我的数据库名称是正确的,但当我单击“下一步”时,问题没有改变,它直接进入问题的结尾/结果,而且问题也不是随机的。我插入数据库的最后一个问题显示在网页中。请帮忙 这是我的代码: <?php require_once('includes/db_conn.php'); $query = "select * from question"; $query_result = $dbc->query($query); $num_questions_retur

我想做一个选择题,问题来自数据库。我确信我的数据库名称是正确的,但当我单击“下一步”时,问题没有改变,它直接进入问题的结尾/结果,而且问题也不是随机的。我插入数据库的最后一个问题显示在网页中。请帮忙

这是我的代码:

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


您的第一个错误是使用$a数组为什么不使用上次for循环和随机问题中使用的$choices句柄$choices数组,而您的“下一步”提交按钮位于表单外部,因此您无法在“下一步”按钮上获取表单值。请将其放入表单标记内部

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


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

您的第一个错误是使用$a数组。为什么不使用在“最后一个for循环”和“随机问题”中所做的$choices数组,而“下一步”的“提交”按钮位于表单外部,因此您无法在表单上获取表单值下一步按钮。将其放入表单标签中

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

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

<?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();

?>