<?php
include '../connect.php';
session_start();

$idAlamat = $_GET['idAlamat'];
$namaPengirim =strtoupper($_POST['namaPengirim']);
$alamat1 =strtoupper($_POST['alamat1']);
$alamat2 =strtoupper($_POST['alamat2']);
$alamat3 =strtoupper($_POST['alamat3']);
$noTel = $_POST['noTel'];
$email = $_POST['email'];

$namaPengirimNew = mysqli_real_escape_string($conn,$namaPengirim);
$alamat1New = mysqli_real_escape_string($conn,$alamat1);
$alamat2New = mysqli_real_escape_string($conn,$alamat2);
$alamat3New = mysqli_real_escape_string($conn,$alamat3);

$ic=$_SESSION["kadPengenalan"];

date_default_timezone_set("Asia/Kuala_Lumpur");			
$date=date('Y-m-d');
$time=date('H:i:sa');

	if($namaPengirim =="" || $alamat1 == "" )
	{
		mysqli_commit($conn);
		echo "<script>alert('Sila lengkapkan maklumat alamat. ');";
		echo "window.location.href = '../daftarAlamat.php';</script>";
		exit;
		
	}
	else
	{
		
		$query="UPDATE `alamat` SET nama_pengirim='".$namaPengirimNew."', alamat1='".$alamat1New."', alamat2='".$alamat2New."', alamat3='".$alamat3New."', no_tel='".$noTel."', email='".$email."' WHERE alamatID='".$idAlamat."' ";
		$strQuery = mysqli_query($conn,$query);
		
		if($strQuery) 
			{
				$sql = "select * from alamat where nama_pengirim='".$namaPengirimNew."'";
				$result = mysqli_query($conn,$sql);
				$row = mysqli_fetch_array($result);
				
				$namaPengirimNew = $row['nama_pengirim'];
				
				$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
					VALUES ('$ic','$date','$time','Daftar','Daftar user')";
				$strQuery1 = mysqli_query($conn,$q4);
				
				if($strQuery1)
				{
					echo "<script>alert('Maklumat alamat berjaya dikemaskini.');";
					echo "window.location.href = '../daftarAlamat.php';</script>";
					exit;
				}

			}
			else
			{
				echo 'MySQL Error: ' . mysqli_error($conn);
				exit;
			}
	}
?>