dimanche 17 février 2019

My php code's if statement that compares input to stored data from mysqli always evaluates to false

I am trying to make a quiz system in php and mysqli but when my code tries to evaluate the input to see if it matches the server data it always evaluates to false. I tried everything I could but I couldn't fix it. Any help would be appreciated.

This is my code.

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" id"postcomments">
<?php
$sqlanswer = "SELECT * FROM questions WHERE question_id like '$guarda'";
$executaranswer=mysqli_query($conanswer, $sqlanswer);
while( $exibiranswer = mysqli_fetch_array($executaranswer)){
    echo '<INPUT TYPE="radio" NAME="answer" VALUE="';
    echo $exibiranswer['alt1'];
    echo '" CHECKED>';
    echo $exibiranswer['alt1'];
        echo '<br>';
    echo '<INPUT TYPE="radio" NAME="answer" VALUE="';
    echo $exibiranswer['alt2'];;
    echo '">';
    echo $exibiranswer['alt2'];
        echo '<br>';
    echo '<INPUT TYPE="radio" NAME="answer" VALUE="';
    echo $exibiranswer['alt3'];
    echo '">';
    echo $exibiranswer['alt3']; 
        echo '<br>';
    echo '<INPUT TYPE="radio" NAME="answer" VALUE="';
    echo $exibiranswer['alt4'];
    echo '">';
    echo $exibiranswer['alt4'];
        echo '<br>';
    echo '<INPUT TYPE="radio" NAME="answer" VALUE="';
    echo $exibiranswer['alt5'];
    echo '">';
    echo $exibiranswer['alt5'];
        echo '<br>';
    echo '<INPUT TYPE="hidden" NAME="question_id" VALUE="';
    echo $guarda;
    echo '">';
    echo '<INPUT TYPE="hidden" NAME="user_id" VALUE="';
    echo $logado;
    echo '">';

}
?>
<input class="formstyletwo" type="submit" name="submit" value="Send">
</form>
<?php
if(isset($_POST['submit'])){
$answer = "";
$question_id = "";
$user_id = "";
$AVATAR = "";
$sqlanswer = "SELECT * FROM questions WHERE question_id like '$guarda'";
$executaranswer=mysqli_query($conanswer, $sqlanswer);
while( $exibiranswer = mysqli_fetch_array($executaranswer)){
// This is the part it goes wrong. //
if ($_POST['answer'] == $exibiranswer['resposta']){                                           
    $finalanswer = 'YES';
} else {
    $finalanswer = 'NO';
}
}
if(isset($_POST["question_id"]))
     $question_id = $_POST["question_id"];
if(isset($_POST["user_id"]))
     $user_id = $_POST["user_id"];

if(isset($finalanswer, $_POST["question_id"], $_POST["user_id"]));


if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

The input is selected by clicking one of the contents on a list, and there is 4 wrong choices, and one correct. But even when I choose the correct one it evaluates to false.
The selected content is stored by its index, and the correct answer is also stored in the database by its index. So there should be no way it would evaluate to false because it is actually a different int.(Normally.)
Thanks for your time.

Aucun commentaire:

Enregistrer un commentaire