单选按钮上提交后的消息

3okqufwl  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(167)

我正在做一个简单的测验。一次显示一个问题,单选按钮上有3个可能的答案。每个问题的答案都存储在一个名为solution的数据库的表中:

我在程序中要做的是显示一条消息“再试一次!”每次用户在点击提交后回答不好的问题。我已经在下面的代码中实现了它,但是我不知道为什么我总是收到“再试一次”的消息当我点击提交。也许是因为我在“空”后面加了一个“else”?或者因为我没有把它和正确的变量比较好?你能帮我找出错误吗?
这是我的密码:

<?php
$Err = "";
$Try = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["choice"])) {
        $Err = "Please select option";
    }
    else{
        if($_POST["choice"] != "solution_number FROM solution WHERE exercise_id_fk=$id"){
            $Try =  "Try again!";
        }
    }
}
?>

<form action='' method='post'>
    <input type="radio" name="choice" value= "1"  /><img src="<?php echo $row["image_path_A"] ?>"/><br>
    <input type="radio" name="choice" value= "2" /><img src="<?php echo $row["image_path_B"] ?>"><br>
    <input type="radio" name="choice" value= "3" /><img src="<?php echo $row["image_path_C"] ?>"><br>
    <br><br>
    <span class="error"><?php echo $Err;?></span>
    <span class="try"><?php echo $Try;?></span>

<br><br><br><!--- Select difficulty --->

    <p2>Select difficulty level:</p2>

    <form action='' method='post'>
        <select name="choose" id="choose">>
        <option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
        <option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
        <option value="3" <?php if($row["difficulty"]=="3") { echo "selected"; } ?> >3</option>
        <option value="4" <?php if($row["difficulty"]=="4") { echo "selected"; } ?> >4</option>
        <option value="5" <?php if($row["difficulty"]=="5") { echo "selected"; } ?> >5</option>
    </select>

        <input class="buttonSubmit" type="submit" name="submit" value="Submit">
        <?php
        if ($next_question_id >= 0) {
        ?>
            <a href="?id=<?php echo $next_question_id; ?>&order=<?php echo $next_question_order; ?>" class="buttonNext" >Next Question</a>
        <?php
        }
        ?>
    </form>

还有我的php:

<?php

if (isset($_POST['submit'])) {
    $user_id = $_SESSION['user_id'];
    $user_check_query = "SELECT * FROM users WHERE id='$user_id'";

    if(isset($_POST['choice'], $_POST['choose'])){
            $choice_answer=$_POST['choice'];
            $difficulty=$_POST['choose'];

          $query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_change, difficulty_student, choice_answer, correct_answer) VALUES ('$id','$user_id', (SELECT IF(difficulty='$difficulty','NO','YES') FROM exercises WHERE exercise_id=$id), '$difficulty', '$choice_answer', (SELECT IF(solution_number='$choice_answer','1','0') FROM solution WHERE exercise_id_fk=$id))";
        $sql=mysqli_query($conn,$query);
    }
}
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题