-
set the correct answers equal to 1?
over 8 years ago
-
over 8 years ago
Hi Nasiya,
I have checked your code , the reason behind your problem that your first answer always gets the value 1, even if you clicked the second radio button is that you have given same value "iscorrect" to both the radio buttons, and you have assigned the value of $_POST['iscorrect'] to a variable $is_correct, no matter which radio button you have selected, $is_correct always have value "iscorrect".
so when you will check the condition 'if($is_correct == "iscorrect")', condition will always true and your first answer always gets 1.
So you need to change the value of your radio buttons, you should give different values to both radio buttons like iscorrect1 and iscorrect2. There is no need of foreach loop and also the condition "if($is_correct == "iscorrect" )" is not true.
The correct condition is:
if($answers[0] != "") { if($is_correct == "iscorrect1" ) { $is_correct = 1 ; }else { $is_correct = 0 ; } $ans_query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('".$lastId."', '".$answers[0]."', '".$is_correct."')") or die(mysqli_error($con)); } if($answers[1] != "") { if($is_correct == "iscorrect2" ) { $is_correct = 1 ; }else { $is_correct = 0 ; } $ans_query = mysqli_query($con,"INSERT INTO answers (question_id, answer, correct) VALUES ('".$lastId."', '".$answers[1]."', '".$is_correct."')") or die(mysqli_error($con)); }
-
1 Answer(s)