Import CSV file data in mysql database using php

1. First you have to create mysql database.
mysql> CREATE DATABASE excel;
 
2. Create table in your choosen database.
 
use DATABASE excel;
 
CREATE TABLE `table_info` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `email` varchar(200) DEFAULT NULL,
  `phone` int(10) DEFAULT NULL,
  `city` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
 
 
3.Open  microsoft excel   create name, email, phone, city  sample  data and ssave that file extension with .csv
4. create A PHP file for uploading CSV file named upload.php
 
upload.php
 
<?php 
 
// database connection
    error_reporting(E_ALL ^ E_DEPRECATED);
        $connect = mysql_connect("localhost","root","");
        $db  =  mysql_select_db('ajax',$connect);
if(isset($_POST["submit"]))
{
 
  if($_FILES['file']['name'])
    {
     $filename=explode('.',$_FILES['file']['name']);
 
if($filename[1]=='csv')
  {
// open the tmp file in read mode
$handle=fopen($_FILES['file']['tmp_name'],"r");
// getting data from csv file
while($data=fgetcsv($handle))
 {
$item1=mysql_real_escape_string($data[0],$connect);
$item2=mysql_real_escape_string($data[1],$connect);
$item3=mysql_real_escape_string($data[2],$connect);
$item4=mysql_real_escape_string($data[3],$connect);
$sql="INSERT into table_info(name,email,phone,city) values('".$item1."','".$item2."','".$item3."','".$item4."')";
mysql_query($sql,$connect);
 
}
fclose($handle);
echo "Data imported sucessfully.";
}
}
  
 
}
 
 
?>
 
<html>
<head></head>
</body>
<form  method="post" enctype="multipart/form-data">
  <div class="form-group">  
    <label for="exampleFormControlFile1">Example file input</label>
    <input type="file" class="form-control-file" name="file">
<input type="submit" name="submit" value="upload">
  </div>
</form>
</body>
</html>