<?php
include("header_bulk_uploads.php");
if(isset($_POST["Import"])){
    
	 $filename=$_FILES["file"]["tmp_name"];
            $failure_data=array();
            $failed_count=0;
		 if($_FILES["file"]["size"] > 0)
		 {
		  	$file = fopen($filename, "r");
		  	$ri=0; $j=0;
	         while (($emapData = fgetcsv($file, 50000, ",")) !== FALSE)
	         {
	             if($emapData==''){
	             
	                 break;
	             }
	           
	             if($ri>0 && $emapData!=''){
	             /******************** Select Pincode Details **************************/
	                    $pincode=$emapData[0];
	                    $statement = "SELECT * FROM `pincode` WHERE pin_code='$pincode'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 
	               if($rowcount==0){  	 
	                   
	                   
	                   if($emapData[4]=="YES"){$emapData[4]="Yes";}else if($emapData[4]=="NO"){$emapData[4]="No";}
	                   if($emapData[5]=="YES"){$emapData[5]="Yes";}else if($emapData[5]=="NO"){$emapData[5]="No";}
	                   if($emapData[6]=="YES"){$emapData[6]="Yes";}else if($emapData[6]=="NO"){$emapData[6]="No";}
	                   if($emapData[7]=="YES"){$emapData[7]="Yes";}else if($emapData[7]=="NO"){$emapData[7]="No";}
	                   if($emapData[8]=="YES"){$emapData[8]="Yes";}else if($emapData[8]=="NO"){$emapData[8]="No";}
	                   if($emapData[9]=="YES"){$emapData[9]="Yes";}else if($emapData[9]=="NO"){$emapData[9]="No";}
	                   
	                   
    	        $statement ="INSERT INTO pincode (pin_code, city, state,location, service, air, road, b2b, b2c,oda) 
    	            VALUES ('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]')";
    	        $result=$conn->query($statement);
	           $j++;
	               }
	               if($ri>0 && ($result==0 ||$rowcount!=0))
				    { 
				      
				        $failure_data[]=$emapData;
				        $failed_count++;
				    }
	 	                   }
				        $ri++;
		    }
			
	         fclose($file);
	          // throws a message if data successfully imported to mysql database from excel file
	        
	            echo "<script type=\"text/javascript\">
	         	alert(\" $j Pincodes successfully Imported.$failed_count Pincodes Failed to Update...\");
	         	 window.location = \"pincode.php\"
					</script>";
			 //close of connection
			mysqli_close($conn); 
				
	}	 
} 
 ?> 