How to import excel data to mysql database in php?
|In this tutorial, we will learn how to import excel to MySQL database.
Many times we face such problems when an organization want to upload data from Microsoft Excel sheet to directly database.
For example all employee database or all file records related to work or all vendor payment done etc.
So, if you have excel file data need to imported into MySQL, you can easily import data easily with PHP by using excel reader API.
Another method– Import excel sheet data to mysql database using phpExcel API.
Steps to import excel to mySQL
First download the php excel reader form the following link – http://sourceforge.net/projects/phpexcelreader/
Lets start writing code for excel file uploading. First we need to create a form for file upload – Lets name is upload.php
<form action=”uploadfile.php” enctype=”multipart/form-data” method=”post”>
<input id=”fileToUpload” name=”fileToUpload” type=”file” />
<input name=”submit” type=”submit” value=”Upload” />
</form>
This form will have action to uploadfile.php, so we will write our import excel php code in this file that will upload excel spreadsheet to MySQL database.
PHP code uploadfile.php –
require_once 'Excel/reader.php'; require 'dbconnect.php'; //Add reader.php to uploadfile.php and don't forget to add db connection file. // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('CP1251'); $data->read($target_file); // $target_file will be your excel file path for ($x = 2; $x < = count($data->sheets[0]["cells"]); $x++) { $name = $data->sheets[0]["cells"][$x][1]; $extension = $data->sheets[0]["cells"][$x][2]; $email = $data->sheets[0]["cells"][$x][3]; $sql = "INSERT INTO mytable (name,extension,email) VALUES ('$name',$extension,'$email')"; echo $sql."\n"; mysql_query($sql); }
if you have a big excel sheet with a big table then you can use –
$sql = "INSERT INTO `mytable` ("; for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { $sql .= "`" . mysql_real_escape_string($data->sheets[0]['cells'][1][$j]) . "`,"; } $sql = substr($sql, 0, -1) . ") VALUES\r\n"; //cells for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { $sql .= "("; for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) { $sql .= "'" . mysql_real_escape_string($data->sheets[0]['cells'][$i][$j]) . "',"; } $sql = substr($sql, 0, -1) . "),\r\n"; } $sql = substr($sql, 0, -3) . ";"; echo '<pre>'; mysql_query($sql); //print_r($data); //print_r($data->formatRecords);
When you run this script then each row will be added to the database dynamically.
Please share your comments if you have any problems.
I have written a post that describes another method to import excel sheet data to mysql database using phpexcel api.
For any help, drop me a message, I will happy to assist you. Thanks..:)
It is explained in a simple and nice way.
Thanks Ajay..:)
nice, but the code :
// ExcelFile($filename, $encoding);
$data = new Spreadsheet_Excel_Reader();
the comments // excelfile….
does not match the following statement. is something missing ?
No, avoid this comment. Thanks .:)
Notice: Undefined variable: target_file. How to declare $target_file
As i mentioned in post that $target_file will be your excel file path.
If you want to help to someone with this code just do it the best way. Did you read all the tutorial that you provide here? You do not explain all the connections. First what are the variables in dbconnect.php? That var $target_file have to be from POST, you also did not provide it. Deprecated: Assigning the return value of new by reference is deprecated in. Warning: require_once(Spreadsheet/Excel/Reader/OLERead.php): failed to open stream: No such file or directory in. Where is that file oleread.php? I`m using XAMPP.
Hi Hacko, thanks for commenting. sometimes you need to debug when you are facing errors in code. Just copy paste never works. I provide the api and how to use with sample code and explained. if you have any specific issue please send me a message by using contact page. i will try to help you.
Hello sir,
I am using this script at my local server but this code was does not execute properly i have four column in my database table name email country and phone number but when i am upload the xls file in my database then email and number value came to the 0 in database table please tell me how i am solve this error.
Check if any error is coming? Try to print the cell values and print the query as well. you are on the half way just need some debugging.
valuable code provided this article for how can upload excel file and view excel file in php .
open demat & Tradiing account and trading tips visit here : http://www.intradaylivetips.com
Hi,
I am able to use it with ‘.xls’ files but it gives error with ‘.xlsx’ file. It says- “The filename is not readable”. Is it possible to use it with ‘.xlsx’ files? any suggestions?
Thanks in advance.
you can try this one http://ecomspark.com/import-data-from-excel-to-mysql-database-using-phpexcel/
It's explained in nice way. working for me but i want cal excel file in dynamically .
for me it is showing there is an error in reader.php file.
it is showing : Parse error: syntax error, unexpected ‘new’ (T_NEW) in C:\localhost\reader.php on line 261
Did you resolve this yet? I am getting the same error. Thanks.
Hello sir, i m working on codeigniter framework i tried the code but its not working for me can u please help me i need a view for user to select the excel file to upload and in controller to read the excel file details and finally save it through model.
I am getting the error “you did not select the file to upload”
oh great
It’s showing me error that “The filename is not readable”
Can you tell me what’s wrong here?
Very nice tutorial, i used this code. Thanks.
Hello Sir,
How can I use this code to read data from more than one excel file.
Thanks for the post. i was looking for the same.
Hi admin, do you monetize your blog ? There is easy method to earn extra money every day,
just search on youtube : How to earn with wordai 4