<?php



$errors = array();
$info = array();
$info_success = array();





//check if action is save_users
//validate the first name
if(!empty($_POST['title']))
{
   $title = mysqli_real_escape_string($dbc, $_POST['title']);

   //collect first name to fill the static form
   $_SESSION['title'] = $title;

}
else
{
   $errors[] = '<p> * </p>';


}



//validate the last name
if(!empty($_POST['description']))
{
   $description = mysqli_real_escape_string($dbc, $_POST['description']);

   $_SESSION['description'] = $description;

}
else
{
   $errors[] = '<p> * </p>';
}

//collect the date
$date = mysqli_real_escape_string($dbc, $_POST['date']);
$_SESSION['date'] = $date;


//validate the added by
if(!empty($_POST['by_name']))
{
   $by_name = mysqli_real_escape_string($dbc, $_POST['by_name']);

   $_SESSION['by_name'] = $by_name;

}
else
{
   $errors[] = '<p> * </p>';
}


//collect the status
$status = (!empty($_POST['status']))? (mysqli_real_escape_string($dbc,$_POST['status'])):
 NULL;
$_SESSION['status'] = $status;




//$security_id = generate_security_code();

//added_by
//$user_id = get_user_id();
//$sid = get_security_id($user_id);


if(empty($errors))
{
	//destroy session if there is no error
	unset($_SESSION['title']);
	unset($_SESSION['description']);
	unset($_SESSION['date']);
	unset($_SESSION['by_name']);
	unset($_SESSION['status']);


	 //check if the query is for adding den add


	$q = "INSERT INTO hostel_sites (id,title, description,date_added, added_by, status)
		 VALUES 			(NULL, '$title','$description', NOW(), '$added_by', '$status')";


	$r = mysqli_query($dbc,$q);

	if($r)
	{
		$info_success['success'] = 'Successfully added!';
		header("Location: gateway.php?act=view_hostel_sites&is=".urlencode($info_success['success']).""); //is - info success

	}
	else
	{
		$errors['error'] =  'User not added due to a system error. We apologize for any inconvenience.';

		header("Location: gateway.php?act=add_hostel_site&error=".urlencode($errors['error'])."");
	}

}
else
{//stay back in add_users form


	$errors['error'] =  'Fill the form appropriately, take note of <span class="ast">*</span>.';

	header("Location: gateway.php?act=add_hostel_site&error=".urlencode($errors['error'])."");

}



?>
