<?php

	require_once 'config.php';
	require_once 'dbh.inc.php';
	require_once 'checkLogin.php';
 
 	// IF USER ENTERS PAGE AFTER 'Delete Anyway' BUTTON HAS BEEN PRESSED (FROM categories.php), DELETE CATEGORY AND EDIT ASSOCIATED QUESTIONS FROM DATABASE
	// ELSE (IF USER ENTERED THIS PAGE WITHOUT SUBMITING A FORM) REDIRECT TO categories.php
	if(isset($_POST['deleteCategory'])) {
	
		// NO NEED TO CLEAN ID, SINCE USER NEVER INPUTS ID
		$id = $_POST['id'];
	
		// DELETE CATEGORY
		$query = "DELETE FROM `category` WHERE `id`='".$id."';";
		if(!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));
		
		// *EDIT EACH QUESTION SUBCATEGORY THAT CORRESPONDED TO PAST CATEGORY (necesita un while?)
		// WARNING: MUST COME BEFORE EDITING QUESTION CATEGORIES
		$query = "UPDATE `question` SET `id_subcategory`='-999' WHERE `id_category`='".$id."';";
		//if(!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));
		
		// *EDIT CORRESPONDING QUESTION'S CATEGORY TO UNKNOWN (necesita un while?)
		// WARNING: MUST COME AFTER EDITING QUESTION SUBCATEGORIES
		$query = "UPDATE `question` SET `id_category`='-999' WHERE `id_category`='".$id."';";
		//if(!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));

		// *DELETE EACH SUBCATEGORY THAT CORRESPONDED TO PAST CATEGORY (necesita un while?)
		$query = "DELETE FROM `subcategory` WHERE `id_categoria`='".$id."';";
		//if(!mysqli_query($connection, $query)) die("Error: ".mysqli_error($connection));
		
	}
	
	header('Location: ../categories.php');
	exit();