<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	require_once('source/restrict_user.php');
	
	if(isset($_POST['check']))
	{
		try {
			$st_pin	= strip_tags($_POST['pin']);
			$st_class	    = strip_tags($_POST['mclass']);
			$st_reg	    = strip_tags($_POST['registration_no']);
			$st_year	    = strip_tags($_POST['year']);
			$st_term	    = strip_tags($_POST['term']);
			
			
			//pin validation
			$STH 				= $DBH->prepare("SELECT count(*) FROM tbl_pin WHERE pin = ? ");
			$result 	   		= $STH->execute(array($st_pin));
			$count_pin	  	= $STH->fetchColumn();
			if($count_pin > 0){  
			
			$rows1 = sql($DBH, "SELECT * FROM tbl_pin where pin = ?", array($st_pin), "rows");							
			foreach($rows1 as $row1)
			{	
				$st_reg_no   		= $row1['reg_no'];
				
			}
			if($st_reg_no == ""){
			sql($DBH, "UPDATE tbl_pin set reg_no = ? WHERE pin = ? ", array($st_reg,$st_pin));
			redirect("my_results.php?msg=results&mypin=$st_pin&term=$st_term&myreg=$st_reg&examyear=$st_year&myclass=$st_class");
			exit;
			}else if($st_reg_no == $st_reg){
			redirect("my_results.php?msg=results&mypin=$st_pin&term=$st_term&myreg=$st_reg&examyear=$st_year&myclass=$st_class");
			exit;
			}else{
			redirect("main.php?msg=sfailed");
			exit;
			}
			}else{
			redirect("main.php?msg=failed");
			exit;
			}
		
		}
		catch(PDOException $e) { 
			file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); # Errors Log File
				redirect('my_results.php?msg=failed');
		}
	}
	else
	{
				redirect('compose_email.php?msg=failed');
	}
?>