Upload CSV File Into MySQLi PHP Script

Upload CSV File Into MySQLi PHP Script

n this tutorial we will try to upload a CSV File Into MySQLi. This simple script can process and insert the CSV file into MySQli Database. It can lessen and fasten your database processing if your csv is already have a data. So let’s do the coding.

Before we started:
First you have to download & install WAMPserver or any local server that run PHP scripts. Here’s the link for WAMP server http://www.wampserver.com/en/.

Creating the database Connection
This is the where the database connection, just simple copy/paste the provided code below.


$conn = new mysqli("localhost", "root", "", "phptut");
die("Fatal Error: Connect Error!");


The Main Interface
This is where the main layout is located, to make this one just simply copy/paste the code below.


require 'connect.php';

<html lang = "en">
<title>PHP - How To Upload CSV Data Into MySQLi</title>
<meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
<link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
<link rel = "stylesheet" type = "text/css" href = "css/jquery.dataTables.css"/>

<nav class = "navbar navbar-default">
<div class = "container-fluid">
<a class = "navbar-brand" href = "<a href="https://sourcecodester.com">Sourcecodester</a>
" rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
</a> </div>
<div class = "col-md-3"></div>
<div class = "col-md-6 well">
<h3 class = "text-primary">PHP - How To Upload CSV Data Into MySQLi</h3>
<hr style = "border-top:1px dotted #000;"/>
<form action = "upload.php" class = "form-inline" method = "POST" enctype = "multipart/form-data">
<div class = "form-group">
<label>CSV File: <input type = "file" name = "file" class = "form-control"/></label>
<button type = "submit" name = "save" class = "btn btn-primary form-control"><span class = "glyphicon glyphicon-upload"></span> UPLOAD</button>
<br />
<table id = "table" class = "table table-bordered">
$query = $conn->query("SELECT * FROM `member`");
while($fetch = $query->fetch_array()){
<td><?php echo $fetch['firstname']?></td>
<td><?php echo $fetch['lastname']?></td>
<td><?php echo $fetch['address']?></td>
<script src = "js/jquery-3.2.1.js"></script>
<script src = "js/jquery.dataTables.js"></script>
<script type = "text/javascript">


The Upload Script
This is where the upload script occur, this script will handle the processing of the CSV file. Then later will be upload to MySQLi database server to be display in a web interface. To do that just simply copy/paste the code below.


require 'connect.php';

$filename = explode(".", $_FILES['file']['name']);
if($filename[1] == 'csv'){
$handler = fopen($_FILES['file']['tmp_name'], "r");
while($data = fgetcsv($handler)){
$conn->query("INSERT INTO `member` (firstname, lastname, address) VALUES('$data[0]', '$data[1]', '$data[2]')");





There you have it we simple Upload a CSV file into the MySQLi database. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorial just kindly visit this site. Enjoy Coding!!



Read Me


Have any Question or Comment?

One comment on “Upload CSV File Into MySQLi PHP Script

… [Trackback]

[…] Read More Info here to that Topic: script.horje.com/view/202973 […]

Leave a Reply

Your email address will not be published. Required fields are marked *