<?php

	require_once 'config.php';
	require_once 'dbh.inc.php';
	require_once 'checkLogin.php';

	// IF USER ENTERS PAGE AFTER 'Save' BUTTON HAS BEEN PRESSED (FROM bankOfQuestions.php), INSERT QUESTION TO DATABASE
	// ELSE (IF USER ENTERED THIS PAGE WITHOUT SUBMITING A FORM) REDIRECT TO bankOfQuestions.php
	if(isset($_POST['insertQuestion'])) {
		
		// ONLY HAVE TO CLEAN PREMISE BECAUSE IT'S THE ONLY RAW USER INPUT, THE REST IS VALID DATA ALREADY FROM DATABASE
		$premise = mysqli_real_escape_string($_POST['question_premise']);
		$id_category = $_POST['question_category'];
		$id_subcategory = $_POST['question_subcategory'];
		$id_type = $_POST['question_type'];
		$id_referencia = $_POST['question_reference'];
		
		$query = "INSERT INTO `question` (`premise`, `id_category`, `id_subcategory`, `id_type`, `id_referencia`) VALUES ('".$premise."', '".$id_category."', '".$id_subcategory."', '".$id_type."', '".$id_referencia."');";
		if (!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));
		
	}
	
	header('Location: ../bankOfQuestions.php');
	exit();