<?php
include("header_bulk_uploads.php");
$courier_id=$_POST["courier_id"];
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, 70000, ",")) !== FALSE)
	         {
	             if($emapData==''){
	                 break;
	             }
	           
	             if($ri>0 && $emapData!=''){
	             /******************** Check AWB CODE Details **************************/
	                    $awbCode=$emapData[0];
	                    $statement = "SELECT * FROM `tbl_awb` WHERE awb_code='$awbCode'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 
	               if($rowcount==0){  	 
    	        $statement ="INSERT INTO tbl_awb (`awb_code`, `status`,`courier_id`) 
    	            VALUES ('$emapData[0]','0','$courier_id')";
    	        $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 AWB Codes successfully Imported.$failed_count AWB Codes Failed to Update...\");
	         	 window.location = \"awbList.php\"
					</script>";
			 //close of connection
			mysqli_close($conn); 
				
	}	 
} 
 ?> 