Home > csv, exel, import data, mysql, php > Import CSV/Excel data into MYSQL database via PHP

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 inHTML form and all data from CSV/Excel must import into MYSQLdatabase 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.

$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());

echo “Successfully Imported”;
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’>


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.

  1. December 10, 2012 at 4:02 am

    Hi! I know this is somewhat off topic but I was wondering
    which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.

  2. alex
    August 28, 2013 at 2:50 pm

    Hello i need codeigniter to upload csv files into database and also data to be inserted

  3. sushant
    October 12, 2013 at 3:32 am

    thanks yar…this script needs some changes..but after making those changes it’s working…I have try to add excel data since 2 days.but I can;t get success.only Due to this script I able to add excel data to the mysql db.really thanks yar…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: