我正在做一个简单的测验。一次显示一个问题,单选按钮上显示3个可能的答案。每个问题的答案都存储在一个名为的数据库的表中 解决方案:
我要在程序中执行的操作是显示“重试!”的消息。每次用户在单击“提交”后回答错误的问题。我已经在以下代码中实现了它,但我不知道为什么总是收到“重试!”的消息。当我单击提交。也许是因为我在“空”之后加上“ 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);
}
}
?>