<?php
	require_once 'conn.php';
 
	
	if(ISSET($_POST['import'])){
		$array=array(
			array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"),
			array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"),
			array("firstname"=>"Tony", "lastname"=>"Stark", "address"=>"Socovia"),
		);
		
		
		$count=count($array);
		$query=$conn->prepare("SELECT COUNT(*) as total FROM `member`");
		$query->execute();
		$row=$query->fetch();
		
		if($row['total']==0){
			for($i=0; $i<$count-1; $i++){
				$firstname=$array[$i]["firstname"];
				$lastname=$array[$i]["lastname"];
				$address=$array[$i]["address"];
				try{
					$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					$sql = "INSERT INTO `member`(firstname, lastname, address)  VALUES ('$firstname', '$lastname', '$address')";
					$conn->exec($sql);
					
				}catch(PDOException $e){
					echo $e->getMessage();
				}
			}
			
			echo"<script>alert('Successfully imported!')</script>";
			echo"<script>window.location='index.php'</script>";
			
		}else{
			echo"<script>alert('An array is already imported!')</script>";
			echo"<script>window.location='index.php'</script>";
		}
		
		
 
	}
	
	
?>