从数据库中获取mcsq'z

时间:2015-09-22 08:22:40

标签: php mysql database

我是一个制作测验的应用程序,它有一个名为 question_bank 的数据库表,它有列:

id | question | a | b | c | d | right_ans | given_ans

我想从数据库中获取所有问题,当用户提交表单时,应将 given_ans与right_ans 进行比较。我能够从数据库中获取所有问题,但它无法正常运行。它应该只从四个给定的选项中选择一个值,但是这段代码允许我选择所有选项。我需要单选按钮的值为a,b,c,d。因为我会将它与right_ans列进行比较。

    $query="SELECT * from question_bank";
    $result= mysqli_query($connection,$query);
    echo "<form method='post' action='exam.php'>";
    while ($read_all_data = mysqli_fetch_assoc($result))
    { 
    $id=$read_all_data['id'];
    $a=$read_all_data['a'];
    $b=$read_all_data['b'];
    $c=$$read_all_data['c'];
    $d=$read_all_data['d'];
     echo $read_all_data['question']."</br>";
     echo "A:<input type ='radio' value ='a'  name='$a' >".$a."</br>";
     echo "B:<input type ='radio' value ='b'  name='$b' >".$b."</br>";
     echo "C:<input type ='radio' value ='c'  name='$c' >".$c."</br>";
     echo "D:<input type ='radio' value ='d'  name='$d' >".$d."</br>";
    }
    echo "<input type='submit' value='submit' name='submit'>"[!

screenshot

3 个答案:

答案 0 :(得分:1)

单选按钮必须具有相同的名称才能使它们互相排斥:

 echo "A:<input type ='radio' value ='a'  name='response".$i."' >".$a."</br>";
 echo "B:<input type ='radio' value ='b'  name='response".$i."' >".$b."</br>";
 echo "C:<input type ='radio' value ='c'  name='response".$i."' >".$c."</br>";
 echo "D:<input type ='radio' value ='d'  name='response".$i."' >".$d."</br>";

答案 1 :(得分:0)

您正在while循环中执行。 然后试试这个:

$i =1;

 while ($read_all_data = mysqli_fetch_assoc($result))
{ 
$id=$read_all_data['id'];
$a=$read_all_data['a'];
$b=$read_all_data['b'];
$c=$$read_all_data['c'];
$d=$read_all_data['d'];
 echo $read_all_data['question']."</br>";
 echo "A:<input type ='radio' value ='a'  name=response'$i' >".$a."</br>";
 echo "B:<input type ='radio' value ='b'  name=response'$i' >".$b."</br>";
 echo "C:<input type ='radio' value ='c'  name=response'$i' >".$c."</br>";
 echo "D:<input type ='radio' value ='d'  name=response'$i' >".$d."</br>";
 $i++;
}

它会使你的电台名称为response1,响应2等等......

答案 2 :(得分:0)

构建表单。

Radio buttons通常组合在一起成组,共享相同name属性的单选按钮是一个组的一部分(例如,一个问题的所有答案)。由于默认情况下未检查任何答案,因此当未在单选按钮组中选中时,不会提交输入。为了防止在组中的第一个单选按钮上使用required属性,这将告诉浏览器仅在输入不为空时才提交表单。

这是一个例子,我在面向对象的风格中使用mysqli

$connection = new mysqli( 'host', 'user', 'pass', 'db' );

$questions = $connection->query( "SELECT * FROM question_bank" );

echo "<form method='POST' action='exam.php'>";
while ( $row = $questions->fetch_assoc() ) {
    echo $row[ 'question' ] . "<br>";
    echo "<input type ='radio' value ='a'  name='$row[id]' required>" . $row[ 'a' ] . "<br>";
    echo "<input type ='radio' value ='b'  name='$row[id]' >" . $row[ 'b' ] . "<br>";
    echo "<input type ='radio' value ='c'  name='$row[id]' >" . $row[ 'c' ] . "<br>";
    echo "<input type ='radio' value ='d'  name='$row[id]' >" . $row[ 'd' ] . "<br>";
}
echo "<input type='submit' value='submit' name='submit'></form>";


处理表单请求。

检查表单是否通过post方法!empty( $_POST['submit'] )(或使用$_SERVER['REQUEST_METHOD'] === 'POST')提交,循环显示所有问题,以便将收到的答案与正确答案进行比较。由于单选按钮使用帖子id作为name属性进行分组,因此$_POST数组将如下所示:

Array
(
    [1] => b
    [2] => c
    [3] => c
    [submit] => submit
)

,其中密钥为问题的id,并且用户选择了value

如何验证表单的示例:

$connection = new mysqli( 'host', 'user', 'pass', 'db' );

if ( !empty( $_POST['submit'] ) ) {
    $questions = $connection->query( "SELECT * FROM question_bank" );

    while ( $row = $questions->fetch_assoc() ) {

        if ( !empty( $_POST[ $row[ 'id' ] ] ) ) {           
            $ans = $row[ $_POST[ $row[ 'id' ] ] ];

            if ( $ans === $row[ 'right_ans' ] ) {
                echo $row[ 'question' ] . ': correct<br>';
            } else {
                echo $row[ 'question' ] . ': incorrect<br>';
            }
        } else {
            echo $row[ 'question' ] . ': no answer<br>';
        }       
    }   
}

如何将给定答案与表格中的正确答案进行比较。

好的,今天我心情很好所以这是一个修改过的例子,你可以将提供的答案保存在数组中,并在构建表单时使用该数组。我还告诉您如何使用ternary operator( $checked && 'd' == $checked ? 'checked' : '' )

在表单提交后检查正确的答案
$answers = [];
$questions = $connection->query( "SELECT * FROM question_bank" );

if ( !empty( $_POST['submit'] ) ) {

    while ( $row = $questions->fetch_assoc() ) {

        if ( !empty( $_POST[ $row[ 'id' ] ] ) ) {           
            $ans = $row[ $_POST[ $row[ 'id' ] ] ];

            if ( $ans === $row[ 'right_ans' ] ) {
                $answers[ $row[ 'id' ] ] = "'$ans' is correct";
            } else {
                $answers[ $row[ 'id' ] ] = "'$ans' is not correct, correct is $row[right_ans]";
            }
        } else {
            $answers[ $row[ 'id' ] ] = 'No answer provided';
        }

    }

}

$questions = $connection->query( "SELECT * FROM question_bank" );

echo "<form method='POST' action='exam.php'>";
while ( $row = $questions->fetch_assoc() ) {

    echo $row[ 'question' ] . "<br>";

    $checked = null;
    if ( isset( $answers[ $row[ 'id' ] ] ) ) {
        echo $answers[ $row[ 'id' ] ] . "<br>";
        $checked = array_search( $row[ 'right_ans' ], array( 'a' => $row[ 'a' ], 'b' =>  $row[ 'b' ], 'c' =>  $row[ 'c' ], 'd' =>  $row[ 'd' ] ) );
    }   

    echo "<input type ='radio' value ='a'  name='$row[id]' required " . ( $checked && 'a' == $checked ? 'checked' : '' ) . ">" . $row[ 'a' ] . "<br>";
    echo "<input type ='radio' value ='b'  name='$row[id]' " . ( $checked && 'b' == $checked ? 'checked' : '' ) . ">" . $row[ 'b' ] . "<br>";
    echo "<input type ='radio' value ='c'  name='$row[id]' " . ( $checked && 'c' == $checked ? 'checked' : '' ) . ">" . $row[ 'c' ] . "<br>";
    echo "<input type ='radio' value ='d'  name='$row[id]' " . ( $checked && 'd' == $checked ? 'checked' : '' ) . ">" . $row[ 'd' ] . "<br>";

}
echo "<input type='submit' value='submit' name='submit'></form>";