import data from excel to mysql using php code – How to Import Excel/CSV file to MySQL Database using PHP?

import data from excel to mysql using php code – import excel file into mysql database tutorial first of all Install PHPExcel with Composer.

import data from excel to mysql using php code

learn import excel file into mysql table with complete source code and explanation. also you can learn to Upload a CSV(excel) file and import into database using PHP and MYSQL.

1.Download Package

So first let’s download from here :

https://github.com/nuovo/spreadsheet-reader

Click Here to download PHP Excel

2.Create connect_db.php file

<?php
	$dbHost = "localhost";
	$dbDatabase = "pakainfo_v1";
	$dbPasswrod = "SD#$%@5665";
	$dbUser = "root";
	$mysqli = new mysqli($dbHost, $dbUser, $dbPasswrod, $dbDatabase);
?>

3.Create index.php file

Therefor let’s copy bellow source code as well as put it into index.php file.
index.php

<!DOCTYPE html>
<html>
<head>
	<title>Excel Uploading PHP - www.pakainfo.com</title>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>


<div class="container">
	<h2>Excel Upload - File</h2>

	<form method="POST" action="fileStoreNew.php" enctype="multipart/form-data">
		<div class="form-group">
			<label>Upload Excel File</label>
			<input type="file" name="file" class="form-control">
		</div>
		<div class="form-group">
			<button type="submit" name="Submit" class="btn btn-success">Upload</button>
		</div>
		<p>Download Demo File from here : <a href="example.ods"><strong>example.ods</strong></a></p>
	</form>
</div>


</body>
</html>

Don’t Miss : import excel file into mysql database tutorial

Read Also:  PHP Convert currency into number to words string to money conversion

4.Create Upload Folder

So, let’s create fileStoreNew.php file and put bellow code:
fileStoreNew.php

<?php
require('library/php-excel-reader/excel_reader2.php');
require('library/SpreadsheetReader.php');
require('connect_db.php');

if(isset($_POST['Submit'])){

  $mimes = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.oasis.opendocument.spreadsheet'];
  if(in_array($_FILES["file"]["type"],$mimes)){

    $uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
    move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
    $Reader = new SpreadsheetReader($uploadFilePath);
    $countAllData = count($Reader->sheets());

    echo "You have total ".$countAllData." sheets".

    $table_view="<table border='1'>";
    $table_view.="<tr><th>Title</th><th>livecomments</th></tr>";

    /* For Loop for all sheets */
    for($i=0;$i<$countAllData;$i++){

      $Reader->ChangeSheet($i);

      foreach ($Reader as $dataResult)
      {
        $table_view.="<tr>";
        $title = isset($dataResult[0]) ? $dataResult[0] : '';
        $livecomments = isset($dataResult[1]) ? $dataResult[1] : '';
        $table_view.="<td>".$title."</td>";
        $table_view.="<td>".$livecomments."</td>";
        $table_view.="</tr>";
        $query = "insert into items(title,livecomments) values('".$title."','".$livecomments."')";

        $mysqli->query($query);
       }
    }

    $table_view.="</table>";
    echo $table_view;
    echo "<br />Data Inserted in MySQL dababase";

  }else { 
    die("<br/>Sorry, File type is not allowed. Only Excel file."); 
  }

}

?>

I hope you get an idea about import data from excel to mysql using php code.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.