<?php
include 'connection_sql.php';
if(isset($_POST["Import"])){ 
      
    $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0) {
        $i=0;
        $file = fopen($filename, "r");
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
             
            if($i >0){
                if($emapData[0] !=""){
                    if($emapData[3] !=""){
                        
                        $sql = "delete FROM members WHERE first_name = '".$emapData[0]."' and last_name = '".$emapData[1]."' and phone = '".$emapData[3]."'";
                        $result = $conn->query($sql);
            
            
                        $sql = "insert into members(first_name,last_name,email,phone) values
                        ('$emapData[0]', '$emapData[1]', '".$emapData[2]."', '".$emapData[3]."')";  echo $sql."<br>" ;
                        $result = $conn->query($sql);
                        
                        $sql = "update s_salma set trans_cancell='0'   ";   
                            $result = $conn->query($sql);
             
                        if(!$result ) { 
        
                            echo "<script type=\"text/javascript\">
                            alert(\"Invalid File:Please Upload CSV File.\");
                            window.location = \"home.php?url=inv_barcode_up\"
                            </script>";
                
                        }
                    }
                }
            }
            $i=$i+1;
    
        }
    fclose($file);
             //throws a message if data successfully imported to mysql database from excel file
     echo "<script type=\"text/javascript\">
                alert(\"CSV File has been successfully Imported.\");
          
            </script>";
        //   window.location = \"home.php?url=inv_barcode_up\"
     //close of connection
    mysqli_close($conn); 
                 
            
    } else{
    echo "<script type=\"text/javascript\">
    window.location = \"home.php?url=inv_barcode_up\"
        </script>";
}
}    else{
    echo "<script type=\"text/javascript\">
    window.location = \"home.php?url=inv_barcode_up\"
        </script>";
}
?>       