Lets say, i want to import/upload excel file to mysql from PHP
My HTML is like below
<form enctype="multipart/form-data" method="post" role="form" action="do.php">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
PHP code is like below
<?php
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '123';
$db= 'dev'; // Database Name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$sql = "INSERT INTO `prayer` (`id` ,`country` ,`city` ,`day` ,`no` ,`datem` ,`fajer` ,`shrok` ,`dohor` ,`aser` ,`maghreb` ,`eshaa`) values ('','ksa','riyadh','$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
//header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
I want to import the values into my table. Its import each row in one field '$emapData[0]'
Please help me how can i solve it or give me any resource.
Edit:-
i check my $emapData content before Array ( [0] => 1;28/6;03:36;05:06;11:57; 3:18;06:47;08:47 ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) CSV File has been successfully Imported
my file like this