How can I import Excel file in mysql? - php

I have a code in which I can import .CSV file in my mysql. But I can not import Excel file. Some garbage values insert in my database while I import Excel file.
<?php
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
include("connection.php");
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 city(city_id,city_name) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
<form enctype="multipart/form-data" method="post" role="form">
<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>

Looks like you are trying to read an excel file using some code that is able to parse only csv files, you can't, the format is completely different.
Check for filetype and use a library like PHPExcel if the user sends you an excel spreadsheet.

You can use EasyXLS Excel library to import Excel file and than to insert data into MySQL database:
$workbook = new COM("EasyXLS.ExcelDocument");
$rows = $workbook->easy_ReadXLSActiveSheet_AsList($filename);
For more details see this link about importing Excel file to MySQL using EasyXLS.

Related

probblem in exporting excel file in php

what is the problem in the code below ? instead that the data is put into the row firstname and lastname the data is join into row firstname . i have sample screenshot in excel , and inserted data in the database , help would be appreciated. thanks
data in the databaseexcel file format
<form enctype="multipart/form-data" method="post" action="import.php" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block"> from excel save us .csv<p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
<?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= '';
$db= 'testdatabase'; // 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");
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$count++;
if($count > 1) {
$sql = "INSERT into person(firstname , lastname) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
echo "Data was successfully added!";
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
My guess is that your problem is there:
while (($emapData = fgetcsv($file, 10000, ";")) !== FALSE)
The problem with Excel creating a CSV file is the dependency with system configuration. I work a lot with french configuration wich is default setuped with ; as delimiter.
The best would be to ask the delemiter instead of hardcoding it in the php script, that's how I do it usually, or you could use the header the find what is between firstname and lastname, that would be your delimiter.

exporting ang retrieving data from excel to database using php

I am trying to create a feature in my program where in you could upload data to the database by uploading data from excel (uploading excel file) i have provided the code below but the data in the database is encoded(i provided a screenshot).
is there something wrong with the code?
Excel format:
firstname lastname
_____________________
amir kumar
jhon doee
But this is what the data looks like when inserted into the database:
This is my index.php:
<form enctype="multipart/form-data" method="post" action="import.php" role="form">
<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>
import.php
<?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= '';
$db= 'testdatabase'; // 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");
$count = 0;
$sql_data = "SELECT * FROM person";
echo $sql_data;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$count++;
if($count > 1) {
$sql = "INSERT into person(firstname , lastname) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
echo "success";
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
Don't use the Excel file as is. You see what gibberish you get.
Instead, do one of these:
"Export" the data from Excel into a .csv file, then LOAD DATA into MySQL.
Use a PHP API that lets you fetch data from Excel. Then INSERT the data into a MySQL table.

uploading excel/CSV file data into database

I have to write a code were i need to import the email ids of people with their names which will be on a excel sheet into the database, but the issue which am facing is, it is inserting blank data into the database,please help, am new to this concept,pardon me if i went wrong somewhere.
DB Structure
import.php
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="200" required="">
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
<?php
if(isset($_POST["Import"]))
{
$con = mysqli_connect("localhost","***","***","***");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$count++;
if($count>1){
$query = "INSERT INTO import_email (via, vault_no, name, email, created_at) VALUES ('".$_SESSION['via']."', '".$_SESSION['vault_no']."', '$name[0]', '$email[1]', NOW())";
mysqli_query($con, $query);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
//header('Location: profile_1.php');
}else{
echo 'Invalid File:Please Upload CSV File';
}
}
?>
EXCEL Structure(.csv format)
Thank you.

How to Import Excel file into mysql Database using PHP

this is my HTML code for the upload form
<form enctype="multipart/form-data" method="post" role="form">
<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>
The PHP code for the connection to the database and the import is below.
<?
mysql_connect("localhost","root","") or die("Unable To Connect");
mysql_select_db("tutorial") or die("Database Not Found");
?>
<?php
if(isset($_POST["Import"]))
{
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();
$count++;
$sql = "INSERT into customers values ('','$emapData[1]','$emapData[1]','$emapData[2]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: test.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
This is what I tried but after I upload , it imports unreadable alphabets into my database table columns
use PHPExcel to import excel or CSV in database https://phpexcel.codeplex.com/

cant upload data from excel into database using php

I cant upload data from excel into database using php and mysql.My code is given below.
Using these code added some unwanted data. Anyone can give me solution for these issue?
HTML:
<form enctype="multipart/form-data" method="post" role="form">
<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
<?php
if(isset($_POST["Import"]))
{
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '';
$db= 'excel'; // 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");
$count = 0;
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
/*print_r($emapData);
exit();*/
$count++;
if($count>1)
{
echo $in="insert into first (name, email) values('$emapData[0]','$emapData[1]')";
$insertTable= mysql_query($in);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
{
echo 'Invalid File:Please Upload CSV File';
}
}
?>

Categories