This is a multiple choice game. It has a web page where you enter a question, which can have multiple answers along with multiple options.
Can anyone recommend any improvements for my code?
//correct & options are both arrays. These arrays are being passed through an AJAX call
function addQuestion($topicID, $difficulty, $isMultiple, $quest,$correct, $options){
global $numRecords,$dbConnection,$stmt;
connect();
//Insert into question table & recieve the question ID for use in the answers insert stmt
$sql = "INSERT INTO question (topic_ID,difficulty,isMultiple, question)
VALUES($topicID,$difficulty,$isMultiple,'$quest');";
try{
$stmt=$dbConnection->query($sql);
$qID = $dbConnection->lastInsertId();
if(!$stmt)
die("error1".$dbConnection->errorInfo());
$stmt = "";
for($i = 0; $i < sizeof($correct); $i++):
$stmt .= "INSERT INTO answer (question_ID, data, isCorrect)
VALUES($qID,'".$correct[$i]."',1);";
endfor;
for($z = 0; $z < sizeof($options); $z++):
$stmt .= "INSERT INTO answer (question_ID, data, isCorrect)
VALUES($qID,'".$options[$z]."',0);";
endfor;
$dd = $dbConnection->query($stmt);
if(!$dbConnection)
die("error2".$dbConnection->errorInfo());
}catch (Exception $e){
//Rollback if fails
$dbConnection->rollback();
return "Error rolling back: ". $e;
}
}