Running an INSERT query inside an If statement

456 views Asked by At

I am wondering if its possible to run an insert query if the answer is correct, I have tried multiple types of queries from creating a new stmt and array and inserting them into the database if the answer if correct, However with my basic understanding of how it all works, I may be missing something? I am not getting any errors returned, just getting the echo statement returned.

Note : I know I am trying to insert QuestionID multiple times its just to put data into the database. It will be updated with the correct fields.

Can anyone enlighten me on whats the issue :

<?php

// establishing the MySQLi connection

require_once('connection-test.php');
$mysqli = new mysqli($host,$username,$password,$database);

if($mysqli -> connect_error)die($mysqli->connect_error);


$questionID = $_POST['id'];
$userAnswer = $_POST['answer'];

$query = "SELECT answers FROM answers WHERE questionID=?";
$statement = $mysqli->prepare($query);
$statement ->bind_param('i', $questionID);

$statement->execute();

$statement->bind_result($answer);


while($statement->fetch()){
    if($answer != $userAnswer){
        echo "Wrong";
        //return to previous Page 

    }else{
       echo "Correct";

        mysqli_query($mysqli,"INSERT INTO submissions (submissionsID,teamID,questionID,answer) 
VALUES ($questionID,$questionID,$questionID,$userAnswer)");


    }


    }


 $statement->close();
?>
1

There are 1 answers

0
Keiththolt On

Turns out I had to free_result()before I could run another mysqli statement