How to Import CSV Into MySQL Database With PHP

0
Shares
How to Import CSV Into MySQL Database With PHP
2.4 (48%) 5 votes

If you’re a PHP developer, then you might have faced this issue right? Importing CSV data and inserting them into MySQL database? For instance, you’ve a CSV file containing hundreds of values and you need to import CSV into MySQL database. Now obviously you can’t copy each data and insert it manually. What you need is a better and quicker solution for this. Solution that doesn’t consume much time.

To overcome this problem, we’ve the right solution. Here, in this PHP tutorial, we’ll show how you can import CSV files easily using PHP into your MySQL database.

Let’s Get Started

Create a file with index.php for HTML inputs.

First create a form.

<html>
<head>
<title>infoblogging => Upload CSV and Insert into Database Using PHP</title>
<head>
<body>
<form method='POST' enctype='multipart/form-data'>
Upload CSV FILE: <input type='file' name='csv_info' /> <input type='submit' name='submit' value='Upload File' />
</form>
</body>
</html>

In this form, define Encrypt method because when the user will upload any image, then “multipart/form-data” method will also compulsory to define.

<form method='POST' enctype='multipart/form-data'>

Now create a database name as “csvdata” and create a new database and write following code.

CREATE TABLE `tbl_csv` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`item1` VARCHAR(50) NOT NULL,
`item2` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB;

After creating database, next step is to create a file and name it as “connection.php” and write following code into it.

$servername = "localhost"; $username = "root"; $password = "ur48x";
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

next, we’ll select the database. write following code for it.

mysqli_select_db($conn,'csvdata');

NOTE :- Here csvdata is our database Name.

Now open “index.php” file and add the following string.

include_once 'connection.php';

Finally, we’ll validate the csv file and then import its records in database.

if(isset($_POST['submit'])){
if($_FILES['csv_data']['name']){

$arrFileName = explode('.',$_FILES['csv_data']['name']);
if($arrFileName[1] == 'csv'){
$handle = fopen($_FILES['csv_data']['tmp_name'], "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

$item1 = mysqli_real_escape_string($conn,$data[0]);
$item2 = mysqli_real_escape_string($conn,$data[1]);
$import="INSERT into tbl_csv(item1,item2) values('$item1','$item2')";
mysqli_query($conn,$import);
}
fclose($handle);
print "Import done";
}
}
}

And done!

Using this method, even large CSV files can be imported easily into MySQL database quickly. So next time, when you require assistance to import CSV into MySQL, while building your business website, you can either use this method or Hire PHP developer to do the job for you.

Grab a free copy of Import CSV PHP tutorial demo from Github.

 
0
Shares
 

Want to Develop Business Website From Scratch? Contact Us Now

One thought on “How to Import CSV Into MySQL Database With PHP

Leave a Reply

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