<?php
SESSION_Start();
?>

<?php
	
	include 'connection.php';

	$PSGC = $_SESSION['GLobal_PSGC'];

	$level1 = $_POST['lvl1_user'];
	$level2 = $_POST['lvl2_user'];
	$level3 = $_POST['lvl3_user'];
	$level4 = $_POST['lvl4_user'];

	$withAccess = $_POST['maykuryente'];
	$withoutAccess = $_POST['walakuryente'];
	$ElectricContributor = $_POST['group2'];
	$electric_powersupply_name = $_POST['elec_name'];
	$RenewableEnergy = $_POST['NumberHouseholdUsingRenewableEnergy'];

	$Landline = $_POST['Landline_Number'];
	$Mobile = $_POST['MobilePhone_Number'];
	$Internet = $_POST['Internet_Number'];

	$Transpo = $_POST['transportation'];

	// Concatenation of Transportation
	$Transport = implode(" | ", (array)$Transpo);


	// Saving households with access to SAFE WATER to accesstosafewater in xampp

		$result=mysqli_query($conn,"Select * from accesstosafewater where PSGC='$PSGC'") or die("Failed to query database ".mysql_error());

		if (mysqli_num_rows($result)>0) {
			mysqli_query($conn, "UPDATE `accesstosafewater` SET `Level_1_WaterSupplySystem`=$level1,`Level_2_WaterSupplySystem`=$level2,`Level_3_WaterSupplySystem`=$level3,`Level_4_WaterSupplySystem`=$level4 WHERE PSGC=$PSGC");
		}

		else {
	  		mysqli_query($conn, "INSERT INTO `accesstosafewater`(`PSGC`,`Level_1_WaterSupplySystem`, `Level_2_WaterSupplySystem`, `Level_3_WaterSupplySystem`, `Level_4_WaterSupplySystem`) VALUES ($PSGC,$level1,$level2,$level3,$level4)");
		}

	// End Saving households with access to SAFE WATER to accesstosafewater in xampp

	// Saving households with and without access to ELECTRICITY: to accesstoelectricity in xampp

		$result=mysqli_query($conn,"Select * from accesstoelectricity where PSGC='$PSGC'") or die("Failed to query database ".mysql_error());

		if (mysqli_num_rows($result)>0) {
			mysqli_query($conn, "UPDATE `accesstoelectricity` SET `WithAccess`=$withAccess,`WithoutAccess`=$withoutAccess,`MainDistributor`='$ElectricContributor',`RenewableUSer`=$RenewableEnergy,`electric_powersupply_name`='$electric_powersupply_name' WHERE PSGC=$PSGC");
		}

		else {
	  		mysqli_query($conn, "INSERT INTO `accesstoelectricity`(`PSGC`,`WithAccess`, `WithoutAccess`, `MainDistributor`, `RenewableUSer`, `electric_powersupply_name`) VALUES ($PSGC,$withAccess,$withoutAccess,'$ElectricContributor',$RenewableEnergy, '$electric_powersupply_name')");
		}


	// End Saving households with and without access to ELECTRICITY: to accesstoelectricity in xampp


	// Saving households with access to the following information & communication technologies to accesstoinformationandcommunicationtech in xampp

		$result=mysqli_query($conn,"Select * from accesstoinformationandcommunicationtech where PSGC='$PSGC'") or die("Failed to query database ".mysql_error());

		if (mysqli_num_rows($result)>0) {
			mysqli_query($conn, "UPDATE `accesstoinformationandcommunicationtech` SET `Landline`=$Landline,`Mobile_Phone`=$Mobile,`Internet`=$Internet WHERE PSGC=$PSGC");
		}

		else {
	  		mysqli_query($conn, "INSERT INTO `accesstoinformationandcommunicationtech`(`PSGC`,`Landline`, `Mobile_Phone`, `Internet`) VALUES ($PSGC,$Landline, $Mobile, $Internet)");
		}


	// End Saving households with access to the following information & communication technologies to accesstoinformationandcommunicationtech in xampp
	


	// Saving modes of transportation to modesoftransportation in xampp

		$result=mysqli_query($conn,"Select * from modesoftransportation where PSGC='$PSGC'") or die("Failed to query database ".mysql_error());

		if (mysqli_num_rows($result)>0) {
			mysqli_query($conn, "UPDATE `modesoftransportation` SET `AvailableTransportation`='$Transport' WHERE PSGC=$PSGC");
		}

		else {
	  		mysqli_query($conn, "INSERT INTO `modesoftransportation`(`PSGC`,`AvailableTransportation`) VALUES ($PSGC,'$Transport')");
		}

	// End Saving modes of transportation to modesoftransportation in xampp
	mysqli_query($conn, "INSERT INTO `modesoftransportation`(`PSGC`,`AvailableTransportation`) VALUES ($PSGC,'$Transport')");

	header('Location:form_adminp8.php');


?>