<?php 
error_reporting(0);
define('INCLUDE_CHECK',true);
session_start();
require ('classes/config.php');
require_once('classes/session.php');

	if(!isset($_POST['fno'])||$_POST['fno']=="") {
			echo "<h1>ERROR!</h1>";
			die();
			exit();
	}

	$pin = htmlspecialchars($_POST['pin']);
	$serial = strtoupper(htmlspecialchars($_POST['serial']));
	$fno=strtoupper(htmlspecialchars($_POST['fno']));
	$ftype=substr($serial,0,2);
	
	$sql=mysql_query("SELECT * FROM admission_result_cards WHERE (pin='$pin' AND serial_no='$serial')LIMIT 1")or die ('An error occured ' . mysql_error());
		while ($row=mysql_fetch_array($sql)){
			$status=$row['status'];
			$usedbyid=$row['usedbyid'];
			$kount = $row['login_count'];
		}
		if(($status=='used' AND $usedbyid == $fno) || ($status=='not-used' AND $usedbyid == '0')){
			$kount=$kount+1;
			$sqlupdate=mysql_query("UPDATE admission_result_cards,admission_cbt_result SET admission_result_cards.status ='used', admission_result_cards.usedbyid ='$fno', admission_result_cards.login_count='$kount',admission_result_cards.log_time= NOW(),admission_cbt_result.date_accessed=NOW() WHERE admission_result_cards.serial_no='$serial' AND admission_cbt_result.formno='$fno'")or die ('An error occured ' . mysql_error());
			$sqli = mysql_query("SELECT formno,pix,surname,othernames FROM admission_students_details WHERE formno = '$fno'");
				while ($rows=mysql_fetch_array($sqli)){
					$_SESSION['fno']=$rows['formno'];
					$_SESSION['surname']=$rows['surname'];
					$_SESSION['othernames']= $rows['othernames'];
					$_SESSION['pix']=$rows['pix'];
				}
			header("location: cbt_scr_result.php");
			exit();
		}else{
			echo "<div class=bg-warning>Used Card / Invalid Pin or Serial Number</div>";
		}
mysql_close();
?>
