Upload CSV File and Insert Records in Database Table Using PHP and MySQL

10-01-2018

Upload CSV File and Insert Records in Database Table Using PHP and MySQL

Import CSV File into MySQL Database Table using PHP. This is very easy in PHP, we can give an upload interface from where the user will upload the CSV file and we can read Records from the CSV file and then we can insert the records one by one into Table.


In PHP, It is common to insert data into a database table from form input. But sometimes, when we need to insert thousands of records at a time, we don't do it from the front-end input form. It will be very time-consuming. We enter our data into a CSV file and then we import this CSV file into the database Table. In the below example, we will see how to Import a CSV file into a database table. Before Starting, we need to create a Database and Table First.


CREATE DATABASE csv_import;

CREATE TABLE users( 
   id INT(10) NOT NULL AUTO_INCREMENT,
   name VARCHAR(200) NOT NULL,
   address VARCHAR(200) NOT NULL,
   PRIMARY KEY(id) );


Code Demo of Importing CSV File in Database Table



	<h2>Import CSV file into sql table</h2>
	<form method="post" action="" enctype="multipart/form-data">
	<b>Upload CSV</b>
	<input type="file" name="file" required>
	<input type="submit" name="upload" value="Import" />
	</form>
	<?php
	  $connection = new MySQLi('localhost','root','','csv_import'); 
	  if(isset($_POST['upload'])){
	  	$csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
	  	 	    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
	  	 	        if(is_uploaded_file($_FILES['file']['tmp_name'])){

	  	 	      //open uploaded csv file with read only mode
	  	 	      $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
	  	 	      //skip first line
	  	 	      fgetcsv($csvFile);
	  	 	      while(($line = fgetcsv($csvFile)) !== FALSE){
	  	 	      	$name  = $line[0];
	  	 	      	$address  = $line[1];
	  	 	      	$sql = "INSERT INTO users SET name = '$name', address = '$address' ";
	  	 	        $connection->query($sql);
	  	 	      }
	  	 
	  	 }
	  	}
	  }
	?>	



Download Code Sample with Database SQL File and CSV File

Latest Published Blog


PurgeCSS - Remove unused CSS from Web Pages

PurgeCSS - Remove unused CSS from Web Pages

03-04-2023

It is generally a good practice to remove unused CSS from web pages. As mentioned earlier, removing unused CSS can provide several benefits, including faster page loading times, improved website performance, reduced page weight, and better code maintainability.


Installing Asterisk16 on CentOs7

Installing Asterisk16 on CentOs7

14-04-2022

Here is Step-by-Step Guide to Installing Asterisk16 on CentOs7.


Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

01-03-2022

Learn How to Make CentOS7 Full Screen Like Primary Operating System in VirtualBox. After Installing CentOS in VirtualBox, CentOs Screen does not Occupy the Full Window Screen. Here in this Tutorial, We Will See how to do it.


Setting  Key to a Column For Quick Fetch Operation From Larze Records

Setting Key to a Column For Quick Fetch Operation From Larze Records

14-02-2019

To set a key to a column in SQL, you need to create an index on that column. An index is a data structure that allows the database to quickly look up records based on the values in the indexed column. By creating an index on a column, you can improve the performance of queries that filter, sort, or group by that column.