<?PHP

if($_FILES["file"]["type"] != "application/vnd.ms-excel"){
	die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
	//Connect to the database
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = 'pulsar99';
	$dbname = 'test';
	$link = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql server');
	mysql_select_db($dbname);
	
	//Process the CSV file
	$handle = fopen($_FILES['file']['tmp_name'], "r");
	$data = fgetcsv($handle, 1000, ";"); //Remove if CSV file does not have column headings
	while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
		$att0 = mysql_real_escape_string($data[0]);
		$att1 = mysql_real_escape_string($data[1]);
		$att2 = mysql_real_escape_string($data[2]);
		$att3 = mysql_real_escape_string($data[3]);
		$att4 = mysql_real_escape_string($data[4]);
		$att5 = mysql_real_escape_string($data[5]);
		$att6 = mysql_real_escape_string($data[6]);
		$att7 = mysql_real_escape_string($data[7]);
		$att8 = mysql_real_escape_string($data[8]);
		$att9 = mysql_real_escape_string($data[9]);
		
		$token = strtok($att1, "T");


		$sql = "INSERT INTO decvalues (date, hrate) VALUES ('" . $token . "', '" . $att6 . "')";
		mysql_query($sql);
	}
	mysql_close($link);
	echo "CSV file successfully imported.";
}
else{
	die("You shouldn't be here");
}
?>
