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

$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);

$kadPengenalan=$_SESSION["kadPengenalan"];

	if($namaPengirim =="" || $alamat1 == "" )
	{
		mysqli_commit($conn);
		echo "<script>alert('Sila lengkapkan maklumat alamat. ');";
		echo "window.location.href = '../daftarAlamat.php';</script>";
		exit;
		
	}
	else
	{
		$query="INSERT INTO alamat (nama_pengirim,alamat1,alamat2,alamat3,no_tel,email)
		VALUES ('".$namaPengirimNew."','".$alamat1New."','".$alamat2New."','".$alamat3New."','".$noTel."','".$email."')";
		$strQuery = mysqli_query($conn,$query);
		
		if($strQuery) 
			{
				date_default_timezone_set("Asia/Kuala_Lumpur");			
				$date=date('Y-m-d');
				$time=date('H:i:sa');
				$q4="INSERT INTO audit_trail(kad_pengenalan,tarikh,masa,operasi,catatan)
						VALUES ('$kadPengenalan','$date','$time','Daftar','Daftar alamat')";
				$strQuery1 = mysqli_query($conn,$q4);
				
				if($strQuery1)
				{
					$sql = "select * from alamat where nama_pengirim='".$namaPengirimNew."'";
					$result = mysqli_query($conn,$sql);
					$row = mysqli_fetch_array($result);
					
					$namaPengirimNew = $row['nama_pengirim'];
					
					echo "<script>alert('Maklumat alamat pengirim berjaya disimpan.');";
					echo "window.location.href = '../daftarAlamat.php';</script>";
					exit;
				}
				else
				{
					echo 'MySQL Error: ' . mysqli_error($conn);
					exit;
				}
			}
			else
			{
				echo 'MySQL Error: ' . mysqli_error($conn);
				exit;
			}
	}
?>