我没有获得计数答案的无线电价值

时间:2018-01-09 06:12:55

标签: php mysqli

我正在尝试使用MySQL,PHP和HTML创建一个测验应用程序。我设计并编写了测验应用程序;但我不知道如何获得无线电值来计算正确和错误的值。我的前端编码是:

<form action="includes/backend_quiz.php" method="post">
    <?php

        $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT $start, $perpage") or die("Could not retrieve data: " .mysqli_error($conn));

            if (mysqli_num_rows($query) > 0){
                $number = 0; 
                    while($row = mysqli_fetch_assoc($query)){           
                        extract($row);  
                        $number++
                        ?>
                        <div class="col-sm-6 col-sm-offset-3">

                            <h4><?php echo $number;?>.&nbsp  <?php echo $quation; ?></h4><br />

                            <input type="radio" name="<?php echo $row['id']?>" value="a" /> <?php echo $opt1;?><br />
                            <input type="radio" name="<?php echo $row['id']?>" value="b" /> <?php echo $opt2;?><br />
                            <input type="radio" name="<?php echo $row['id']?>" value="c" /> <?php echo $opt3;?><br />
                            <input type="radio" name="<?php echo $row['id']?>" value="d" /> <?php echo $opt4;?><br />
                            <br />
                        </div>
                        <?php
                    }       
            }   
    ?>
    <div class="col-sm-6 col-sm-offset-3">
        <button class='btn btn-success' type='submit' name='ans'>Finish</button>        
    </div>
</form>

输出

Quiz Application

数据库设计

quiz table

4 个答案:

答案 0 :(得分:0)

    <form action="includes/backend_quiz.php" method="post">
        <?php

            $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT $start, $perpage") or die("Could not retrieve data: " .mysqli_error($conn));

                if (mysqli_num_rows($query) > 0){
                    $number = 0; 
                        while($row = mysqli_fetch_assoc($query)){           
                            extract($row);  
                            $number++
                            ?>
                            <div class="col-sm-6 col-sm-offset-3">

                                <h4><?php echo $number;?>.&nbsp  <?php echo $quation; ?></h4><br />

                                <input type="radio" name="ans_<?php echo $row['id']?>" value="a" /> <?php echo $opt1;?><br />
                                <input type="radio" name="ans_<?php echo $row['id']?>" value="b" /> <?php echo $opt2;?><br />
                                <input type="radio" name="ans_<?php echo $row['id']?>" value="c" /> <?php echo $opt3;?><br />
                                <input type="radio" name="ans_<?php echo $row['id']?>" value="d" /> <?php echo $opt4;?><br />
                                <br />
                            </div>
                            <?php
                        }       
                }   
        ?>
        <div class="col-sm-6 col-sm-offset-3">
            <button class='btn btn-success' type='submit' name='ans'>Finish</button>        
        </div>
    </form>


On php file backend_quiz.php you can get using

<?php echo $_POST['ans_'.$questionId]; ?>

will get value of radio button you have selected

答案 1 :(得分:0)

我从你的问题中理解的是 1.您希望得到用户选择的答案 2.检查答案是否与数据库值

中的正确答案匹配

对于1,你可以使用Geshode提到的$ _POST $ row [id]。
对于2我会做这样的事情

  1. 将所有问题ID和所选答案存储在提交
  2. 的数组变量中
  3. 将用户答案与数据库正确答案进行比较,例如
    SELECT * FROM quiz_table WHERE answer=$user_ans;
  4. 如果计数匹配,你知道这是正确的答案,现在你可以像得分++或其他什么。

答案 2 :(得分:0)

     <form action="includes/backend_quiz.php" method="post">
    <?php

        $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT $start, $perpage") or die("Could not retrieve data: " .mysqli_error($conn));

            if (mysqli_num_rows($query) > 0){
                $number = 0; 
                    while($row = mysqli_fetch_assoc($query)){           
                        extract($row);  

                        ?>
                        <div class="col-sm-6 col-sm-offset-3">

                            <h4><?php echo $number;?>.&nbsp  <?php echo $quation; ?></h4><br />

                            <input type="radio" name="question[<?php echo $number?>]" value="a" /> <?php echo $opt1;?><br />
                            <input type="radio" name="question[<?php echo $number?>]" value="b" /> <?php echo $opt2;?><br />
                            <input type="radio" name="question[<?php echo $number?>]" value="c" /> <?php echo $opt3;?><br />
                            <input type="radio" name="question[<?php echo $number?>]" value="d" /> <?php echo $opt4;?><br />
                            <br />
                        </div>
                        <?php
                        $number++;
                    }       
            }   
    ?>
    <div class="col-sm-6 col-sm-offset-3">
        <button class='btn btn-success' type='submit' name='ans'>Finish</button>        
    </div>
</form>

要访问发布的值,您只需使用$ _POST [&#39;问题&#39;]

考虑选择5个问题选项1,选项2,选项3,选项1,选项2 POST [&#39;问题&#39;]将给出类似

  

数组([1] =&gt; a [2] =&gt; b [3] =&gt; c [4] =&gt; a [5] =&gt; b)

要从此数组中访问单个值,可以将foreach循环用作

<?php 
 foreach($_POST['question'] as $option_num => $option_val)
    echo $option_num." ".$option_val."<br>";
?>

答案 3 :(得分:0)

您应该将答案作为数组发送。

以下是更正后的代码。希望它有效

<form action="includes/backend_quiz.php" method="post">
    <?php

        $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT $start, $perpage") or die("Could not retrieve data: " .mysqli_error($conn));

            if (mysqli_num_rows($query) > 0){
                $number = 0; 
                    while($row = mysqli_fetch_assoc($query)){           
                        extract($row);  
                        $number++
                        ?>
                        <div class="col-sm-6 col-sm-offset-3">

                            <h4><?php echo $number;?>.&nbsp  <?php echo $quation; ?></h4><br />

                            <input type="radio" name="answers[<?php echo $row['id']?>]" value="a" /> <?php echo $opt1;?><br />
                            <input type="radio" name="answers[<?php echo $row['id']?>]" value="b" /> <?php echo $opt2;?><br />
                            <input type="radio" name="answers[<?php echo $row['id']?>]" value="c" /> <?php echo $opt3;?><br />
                            <input type="radio" name="answers[<?php echo $row['id']?>]" value="d" /> <?php echo $opt4;?><br />
                            <br />
                        </div>
                        <?php
                    }       
            }   
    ?>
    <div class="col-sm-6 col-sm-offset-3">
        <button class='btn btn-success' type='submit' name='ans'>Finish</button>        
    </div>
</form>

并且在服务器上,您可以使用$_POST['answers'][$question_id]

获取值