How To Import CSV File into MySQL using PHP

How To Import CSV File into MySQL using PHP

In this Post We Will Explain About is How To Import CSV File into MySQL using PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to Import and Export CSV Files Using PHP and MySQL Example

In this post we will show you Best way to implement How to Import CSV File Data into MySQL Database using PHP, , hear for How To Import CSV File Into MySQL Using PHP with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Also Read :  Import csv file data into mysql database using php

How to import CSV in PHP MySQL Example

fgetcsv() PHP method is used to All the read data from some CSV file. First of all make an any excel sheet and root path to save it as .csv format
Here We can download CSV File Demo.
Example to some data import CSV file and MySQL save to database.

index.html

<html>
 <body>
<form method="post" enctype="multipart/form-data">
<table cellpadding="0" cellspacing="0" border="0" id="ad_tab">

<tr>
<td class="req">File name to import : </td>
 
<td><input type="file" name="filename" size="50"></td>
</tr>
<tr>
<td>
<input type="submit" value="Upload" name="data_submit"></td>
<tr>
</table>
</form>
</body>
</html>

submit.php

if(isset($_POST['data_submit'])) {
		$Live_data=$_FILES['filename'];
		if(!$Live_data['size'])
		$err="please any select the file";
		elseif(is_uploaded_file($_FILES['filename']['tmp_name'])){
		$handle = fopen($_FILES['filename']['tmp_name'], "r");
		$live_csvformat="Name,Email,Address,Hobby,Country,Mobile,Course\n";
		$hasNext=1;
		$i=date('Ymdhis',time());
		$fileName="error".$i.".csv";
		$fh=fopen($fileName,"a+");
		while (($Live_data = fgetcsv($handle, 1000, ",")) !== FALSE) {
		IF($Live_data[0]==""||$Live_data[1]==""||$Live_data[2]==""||$Live_data[3]==""||$Live_data[4]==""||$Live_data[5]==""||$Live_data[6]==""){
		$live_csvformat.=$Live_data[0].",".$Live_data[1].",".$Live_data[2].",".$Live_data[3].",".$Live_data[4].",".$Live_data[5].",".$Live_data[6]."\n";
		 
		}
		else{
		if($hasNext) 
		{
			$import="INSERT into infomation
			(name,email,address,hobby,country,mobile,course) values('$Live_data[0]','$Live_data[1]','$Live_data[2]','$Live_data[3]','$Live_data[4]','$Live_data[5]','$Live_data[6]')";
			mysql_query($import);
		}
			}
	$hasNext++;
	}
	$err="Good Luck..!!1Import Done";
	fwrite($fh,$live_csvformat);
	fclose($fh);
	}
}

Example

Also Read :  Insert Update Delete view function using PHP and mysql - crud function

I hope you have Got What is How to Import CSV File Data Into Mysql Using PHP And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Recommended For You.

Laravel Validation Unique Soft Delete Example
Today, We want to share with you Laravel Validation Unique Soft Delete Example.In this post we will show you laravel

Add a Comment

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