<?php error_reporting(0); ?>


<?php
$host = "localhost";
$username = "root"; 
$password = ""; 
$db = "myUHM"; 
?>

<?php /*?>
<?php
$host = "localhost";
$username = "mygoldpl_mygoldplt"; 
$password = "295@pS7(F4"; 
$db = "mygoldpl_mygoldplt"; 
?>
<?php */?>
<?php
$mysqli = new mysqli($host,$username,$password,$db);
?>

<?php
$sql_user = "SELECT *
FROM user
WHERE ref_code='MGP10001'
And status='1'
LIMIT 1";
$result_user = $mysqli->query($sql_user);
	if ($result_user->num_rows > 0)
	{
		while($row_user = $result_user -> fetch_assoc())
		{                
			$id = $row_user['id'];
			$ref_code = $row_user['ref_code'];
			$ic_front = $row_user['ic_front'];
?>
<form method="post" action="" onsubmit="return confirm('Do you want to proceed?');" enctype="multipart/form-data">
	<label class="control-label">Test Upload</label>
	<?php if ($ic_front != "")
	{
	?>
	| <a href="../uploads/<?php echo $ic_front; ?>" download><font color="blue">Download</font></a> 
	<br> 
	<img src="../uploads/<?php echo $ic_front; ?>" width="100" height="100" class="img-circle">
	<?php
	}
	?>
	<br><br>
	<b><font size="1px">Only Jpg, Jpeg and Png file allow to be upload</font></b>
	<input type="file" class="form-control" name="ic_front">

	<input type="hidden" class="form-control"  name="ref_code" value="<?php echo $ref_code; ?>">

	<button name="Update_User_Detail" class="btn btn-success"><i class="fa fa-check"></i> Update</button>
</form>
<?php
	}
}
?>


<?php
if (isset($_POST['Update_User_Detail'])) 
{
	$ref_code = $_POST['ref_code'];

	date_default_timezone_set("Asia/Kuala_Lumpur");

	$mysqli->begin_transaction();

	if ($_FILES['ic_front']['size'] != 0 )
	{
		$target_dir = "../uploads/";
		$time_stamp = time();
		$temp1 = $temp1 = explode(".", $_FILES["ic_front"]["name"]);
		$newfilename1 = $ref_code . $time_stamp. '1.' . end($temp1);
		$target_file1 = $target_dir . basename($newfilename1);
		$uploadOk = 1;
		$imageFileType1 = pathinfo($target_file1,PATHINFO_EXTENSION);

		// Check if image file is a actual image or fake image
		if(isset($_POST["submit"]))
		{

			$check1 = getimagesize($_FILES["ic_front"]["tmp_name"]);
			if( ($check1 !== false) )
			{
				echo "File is an Image - " . $check1["mime"] . ".";
				$uploadOk = 1;
			} 
			else 
			{
				$mysqli->rollback();
				echo "<script type='text/javascript'>alert('File is not an Image.');</script>";
				$uploadOk = 0;
				echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
				exit();
			}
		}

		// Allow certain file formats
		if($imageFileType1 != "jpg" && $imageFileType1 != "JPG" && $imageFileType1 != "png" && $imageFileType1 != "PNG" && $imageFileType1 != "jpeg" && $imageFileType1 != "JPEG")
		{
			$mysqli->rollback();
			echo "<script type='text/javascript'>alert('Sorry, only JPG, JPEG, PNG files are allowed.');</script>";
		    $uploadOk = 0;
			echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
			exit();
		}

		// Check if file already exists
		if ( (file_exists($target_file1)) )
		{
			$mysqli->rollback();
			echo "<script type='text/javascript'>alert('Sorry, file already exists.');</script>";
			$uploadOk = 0;
			echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
			exit();
		}

		// Check if $uploadOk is set to 0 by an error
		if ($uploadOk == 0)
		{
			$mysqli->rollback();
			echo "<script type='text/javascript'>alert('Sorry, your file was not uploaded.');</script>";
			echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
			exit();
			// if everything is ok, try to upload file
		}
		else
		{
			if (move_uploaded_file($_FILES["ic_front"]["tmp_name"], $target_file1))
			{
				$ic_front = $ref_code . $time_stamp. '1.' . end($temp1);

				$UpdateQuery = "UPDATE user SET 
				ic_front='$ic_front'
				WHERE ref_code='$ref_code' ";
				$result = $mysqli->query($UpdateQuery);

				$mysqli->commit();
				echo "<script type='text/javascript'>alert('Succesfully Update');</script>";
				echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
				exit();
			}
			else
			{
				$mysqli->rollback();
				echo "<script type='text/javascript'>alert('Sorry, there was an error uploading your file.');</script>";
				echo "<script type='text/javascript'> document.location='Test_upload_file.php'; </script>";
				exit();
			}
		}
	}
}
?>