<?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, 70000, ",")) !== FALSE)
	         {
	             if($emapData==''){
	                 break;
	             }
	           
	             if($ri>0 && $emapData!=''){
	             /******************** Check HSN CODE Details **************************/
	                    $hsncode=$emapData[0];
	                    $statement = "SELECT * FROM `tbl_hsncode` WHERE hsncode='$hsncode'";
	                 	$response=mysqli_query($conn,$statement);
	                 	$rowcount=mysqli_num_rows($response);
	                 
	               if($rowcount==0){  	 
    	        $statement ="INSERT INTO tbl_hsncode (`hsncode`, `igst`, `cgst`, `sgst`) 
    	            VALUES ('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
    	        $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 HSN Codes successfully Imported.$failed_count HSN Codes Failed to Update...\");
	         	 window.location = \"hsnCode.php\"
					</script>";
			 //close of connection
			mysqli_close($conn); 
				
	}	 
} 
 ?> 