Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Import Excel File Into MySQL Using PHP

Friday, Jan 27, 2017

Tags:- PHP MySQL


In this tutorial we will show you how to import excel file data into mysql database using PHP. Generally this process is very hard if you directly import excel file data into mysql database but now by converting excel file into a csv(comma separated value) file then import data into mysql database this becomes easy and time saving here in this tutorial we will follow this step.


Import Excel File Into MySQL Using PHP


To Import Excel File Into MySQL It Takes Only Two Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to import data into mysql database


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name import.html

<html>
<body>
<div id="wrapper">
 <form method="post" action="import_file.php" enctype="multipart/form-data">
  <input type="file" name="file"/>
  <input type="submit" name="submit_file" value="Submit"/>
 </form>
</div>
</body>
</html>

In this step we create a form to send csv file to 'import_file.php' page after manually converting from excel file.



Step 2.Make a PHP file to import data into mysql database

We make a PHP file and save it with a name import_file.php

// Database Structure 
CREATE TABLE 'employee' (
 'name' text NOT NULL,
 'age' text NOT NULL,
 'country' text NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
if(isset($_POST["submit_file"]))
{
 $file = $_FILES["file"]["tmp_name"];
 $file_open = fopen($file,"r");
 while(($csv = fgetcsv($file_open, 1000, ",")) !== false)
 {
  $name = $csv[0];
  $age = $csv[1];
  $country = $csv[2];
  mysql_query("INSERT INTO employee VALUES ('$name','$age','country')");
 }
}
?>

In this step we create a database 'employee' to store employee details which is stored in csv file.We get the file and we use fopen to read the csv file and then we use fgetcsv function in while loop to get comma seperated value one by one and insert in database.


That's all, this is how to import excel file data into mysql database using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH