 <?php 





 
 include('connect-db.php');
//connect to the database
//$connect = mysql_connect("localhost","username","password");
//mysql_select_db("mydatabase",$connect); //select the table
//
// get results from database
	$result = mysql_query("SELECT * FROM tb_login") 
		or die(mysql_error());  
		

 function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
		/*
if ($_FILES[csv][size] > 0) {

    //get the csv file
    $csvFile = $_FILES[csv][tmp_name];
    }
	
	*/
		
// Set path to CSV file
$csvFile = '/var/www/html/xadesweb/protected/contacts.csv';

//calling the function
$csv = readCSV($csvFile);
if(!empty($csv)){
    foreach($csv as $file){
        //inserting into database
        $query_insert = "insert into tb_login set 
            username    =   '".$file[0]."',
            company   =   '".$file[1]."',
			 companyname    =   '".$file[2]."',
			  active    =   '".$file[3]."',
			   status    =   '".$file[4]."',
			    firstname    =   '".$file[5]."',
				 lastname    =   '".$file[6]."',
				  email    =   '".$file[7]."',
				   password    =   '".$file[8]."";		   
			
            echo $query_insert;
        $insert = mysql_query($query_insert);   
  }
}else{
   echo 'Csv is empty'; 
    
}


 
 include('connect-db.php');
//connect to the database
//$connect = mysql_connect("localhost","username","password");
//mysql_select_db("mydatabase",$connect); //select the table
//
// get results from database
	$result = mysql_query("SELECT * FROM tb_login") 
		or die(mysql_error());  
		

	


    //redirect
    header('Location: import.php?success=1'); die;

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  Choose your file: <br />
  <input name="csv" type="file" id="csv" />
  <input type="submit" name="Submit" value="Submit" />
</form>

</body>
</html>
