Import CSV/Excel data into MYSQL database via PHP
Hello Friends,
Today i came across a functionality where i need to import the CSV/Excel file in to MYSQL database via PHP script. There is a special requirement from client where he can upload the CSV/Excel file in file upload field in HTML form and all data from CSV/Excel must import into MYSQL database table through PHP.
You can import data of CSV/Excel into MYSQL via PHP using fgetcsv() function along with some file handling functions. Here i would like to share that script with all of you. I hope that in future this article will be helpful to you when you need to implement this type of functionality.
if(isset($_POST['SUBMIT']))
{
$fname = $_FILES['sel_file']['name'];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into user(name,email,phone) values('$data[0]','$data[1]','$data[2]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>
Import File : <input type='text' name='sel_file' size='20'>
<input type='submit' name='submit' value='submit'></form>
Above code will first check for valid csv file. If it is valid csv file than with the use of fopen() function , uploaded file will be opened in read mode. Now using fgetcsv() function , you will have a line by line data from csv file. Each line you will get is an array with all column values. So now you have all data from csv file. You can play with them according to your needs. I have shown an example to insert 3 data in database table user. If your csv file contains more data than you will get in $data[0] , $data[1], $data[2],$data[3] and so on..
Thats it. Let me know your thoughts for the same. If you face any problem in this than let me know via comment or contact us form.
To know more about programming,JavaScript issues,jQuery,Expression Engine,MYSQL database and Open-source, enter your email address below. We will send you free tutorials.
- abcphp.com
- Import CSV/Excel data into MYSQL database via PHP | phpfreelancedevelopers
- Sell und buy online
- It is the digital spotlight forum
- Joomla|Joomla templates| Joomla tutorials|Joomla tips|Joomla blog|Joomla extensions
- OpenCart | SanalPos | opencart | E-Ticaret | Opencart | Tasarımları
- wso reviews
- Photostand, image host, image hosting
- Series Gratis Online
- facebook profiles
- Arvixe Review
- Dich Vu Thiet Ke Web Uy Tin – Chat Luong – Chi Phi Hop Ly
- Small Business SEO Service
- cms dle
- kho phim vip
- email addresses
- φωτοβολταικά κόστος






I am
about 2 years ago
thank you…
about 2 years ago
thank you…
about 2 years ago
What if i have large amount of data in my CSV file.It will surely hamper performance in this case.
about 2 years ago
What if i have large amount of data in my CSV file.It will surely hamper performance in this case.
about 2 years ago
Hi
This code is not working.
about 2 years ago
Hi
This code is not working.
about 2 years ago
Hi Vasu,
Can i know where you are facing problem in above code ? I can import CSV data into MYSQL database successfully with this code.
Thanks,
Rakshit Patel.
about 2 years ago
Hi Vasu,
Can i know where you are facing problem in above code ? I can import CSV data into MYSQL database successfully with this code.
Thanks,
Rakshit Patel.
about 2 years ago
i need import excel file to table in php.
The above example not showing excel data properly from import file. Plz help me.
about 2 years ago
i need import excel file to table in php.
The above example not showing excel data properly from import file. Plz help me.
about 2 years ago
Dear,
if you want to import Excel into mySQL – http://dbTube.org is the right tool.
Greetings
Andras
about 2 years ago
Dear,
if you want to import Excel into mySQL – http://dbTube.org is the right tool.
Greetings
Andras
about 2 years ago
@Andras
Hi,
dbTube is working well but you must pay 4.99 Euro for the software.
Advantage: sourcecode available. In this case in can adapt the software
to my requriements (cron job for automatic import)
about 2 years ago
@Andras
Hi,
dbTube is working well but you must pay 4.99 Euro for the software.
Advantage: sourcecode available. In this case in can adapt the software
to my requriements (cron job for automatic import)
about 2 years ago
Hi

I need to import daily and multiple instances of realy big .csv files, like 330k lines, in an online MySQL tables with no ; at the line end so the "bigdump.php" way is not for me.
Cant found any way until now to accomplish this with PHP
Please if somebody know something what is working I will realy apreciate
An example for the file is : http://roc1.triburile.ro/map/village.txt.gz - 4.6MB comprimed, expanded 14MB
about 2 years ago
Hi

I need to import daily and multiple instances of realy big .csv files, like 330k lines, in an online MySQL tables with no ; at the line end so the "bigdump.php" way is not for me.
Cant found any way until now to accomplish this with PHP
Please if somebody know something what is working I will realy apreciate
An example for the file is : http://roc1.triburile.ro/map/village.txt.gz - 4.6MB comprimed, expanded 14MB
about 1 year ago
hi, the code doesn't work for me either
about 1 year ago
hi, the code doesn't work for me either
about 1 year ago
Whats the problem you are facing ?
about 1 year ago
Whats the problem you are facing ?
about 1 year ago
Thanks
about 1 year ago
Thanks
about 1 year ago
I received the following error while trying to import a large css file :
“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Ivoire’)’ at line 1″
about 1 year ago
I received the following error while trying to import a large css file :
“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Ivoire’)’ at line 1″
about 1 year ago
Its a MYSQL Query error. If you can post the MYSQL Query than i will give you idea where the problem is.
about 1 year ago
Its a MYSQL Query error. If you can post the MYSQL Query than i will give you idea where the problem is.
about 1 year ago
Hello friend,
This function is working properly and perfect but dear we need to import data .XLSX or .XLS to mysql so how its possible if i am using this function for that then its return junk characters to please write a easily program for that.
Regards,
Nilesh Gupta
about 1 year ago
Hello friend,
This function is working properly and perfect but dear we need to import data .XLSX or .XLS to mysql so how its possible if i am using this function for that then its return junk characters to please write a easily program for that.
Regards,
Nilesh Gupta
about 1 year ago
Thanks for compliment.
about 1 year ago
do you have any idea to solve memory limit issue for large csv file importing? i’m finding a solution…
about 1 year ago
csv file function -> fgetcsv() function
then
excel file function -> what?
Thanks a lot
Venkatesh
about 1 year ago
Goood Well done
Thanks
about 1 year ago
wt to do in case of excel file..extenshion .xls..????
about 1 year ago
Hello
do u have any idea to solve –when we upload file excel file and save into data base some field name,email, marks…
about 1 year ago
hi there…its a good script, but i have a problem with that, the problem is if there’s ” ‘ ” or ” ” ” in my data such as ” honda’90 ” or ” welcome to “ABC” shop ” it will occur an error…can you help me with this problem??? thanks before…
Erik
about 1 year ago
This won’t work if the filename is “my.import.file.csv”.
$chk_ext = explode(“.”,$fname);
if(strtolower($chk_ext[1]) == “csv”)
To get the extension of the file use:
$chk_ext = strrchr($fname, ‘.’);
about 1 year ago
Thanks for information Fallen. I think end($chk_Ext) will also works. Anyways great thanks..
about 10 months ago
The code is not working.
Whenever i select a .csv file to upload It says Invalid file.
about 9 months ago
What if you gave us some downloadable sample to implement this code, for newbie like us it will be much easier to understand
what about saving the loaded data to database?
about 9 months ago
i have uploaded product_list.csv file on the server but result shows invalid file. any one would like to help me?
about 9 months ago
it is showing invalid file error
about 9 months ago
The code is not working. Then I edited the sel_file
type to file from text then also its not working.
about 8 months ago
Some of you are facing the “Invalid File” error because if you’re observant enough, the sample code above is missing the —–enctype=”multipart/form-data”—— =)
about 8 months ago
Here i think we need to add enctype=”multipart/form-data” in the form
about 7 months ago
I found this code very buggy as well. I spent more time trying to fix it until finally I rewrote it the following is my solution to the same function. I kept receiving an error that the resource was invalid. My only guess is the something was happening to the temp file created by php. I tried uploading and saving the file and kept receiving the same error. This is the solution that finally worked for me. This is only the csv parsing not the file checking for the file extention.
about 7 months ago
or if you want to skip any steps I made this utility available
about 7 months ago
Very useful. I modified it according to my needs and it worked beautifully.
Thank you very much!
about 7 months ago
idiot ho kya ekdum!!!
$_FILES[][] usekar liya and form me koi file control hai hin nahi!!!
kya hai yaar!!! Gahnta upload hogi file kya???
about 7 months ago
i want to import excel data with PHPcode and save it in database pleas send me proper code for that
Thanx
about 6 months ago
Thank you so much. Very useful code. Works very well for CSV files.
Could you please tell me the changes to be made in this code so that it can import EXCEL files as well? When I tried using the same code, some junk data is added to the database.
Thanks again. Waiting for your response.
Ajna