PHP MySQLi Import CSV File Data into MySQL Database

PHP MySQLi Import CSV File Data into MySQL Database

In this Post We Will Explain About is PHP MySQLi Import CSV File Data into MySQL Database With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Import CSV File Data Using MySql And PHP-MySQLi Example

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




Also Read :   How to display/load JSON File using PHP with MySQLi

Creating Simple Form(index.php)

Make a new file php file and name Like main root file it as index.php as well as simple copy paste the below source codes.

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Step By step Import CSV Data File into MySQL Database using PHP/MySQLi Example</title>
	<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="live24u container">
	<h1 class="page-header text-center">PHP Mysqli - Import CSV File into MySQL Database</h1>
	<div class="row">
		<div class="col-sm-3">
			<h3>PHP Mysqli step by tspe Import File Form</h3>
			<form method="POST" action="do_import.php" enctype="multipart/form-data">
				<div class="like form-group">
					<label for="file">File:</label>
					<input type="file" id="file" name="file">
				</div>
				<button type="submit" name="import" class="btn btn-primary btn-sm">Import</button>
			</form>
			<?php
				session_start();
				if(isset($_SESSION['logMsg'])){
					?>
					<div class="live24u alert alert-info text-center" style="margin-top:20px;">
						<?php echo $_SESSION['logMsg']; ?>
					</div>
					<?php
 
					unset($_SESSION['logMsg']);
				}
 
			?>
		</div>
		<div class="col-sm-9">
			<table class="live24u table table-bordered table-striped">
				<thead>
					<th>UserID</th>
					<th>Stude Fname</th>
					<th>Stude Lname</th>
					<th>Student Address</th>
				</thead>
				<tbody>
					<?php
					//database connection
					$db_con = new mysqli('localhost', 'root', '', 'mydatabase');
 
					$sql = "SELECT * FROM students";
					$query = $db_con->query($sql);
 
					while($row = $query->fetch_array()){
						?>
						<tr>
							<td><?php echo $row['id']; ?></td>
							<td><?php echo $row['studFname']; ?></td>
							<td><?php echo $row['studLname']; ?></td>
							<td><?php echo $row['address']; ?></td>
						</tr>
						<?php
					}
 
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
</body>
</html>

Creating Simple Import Script

Last step, we Make simple file upload/import data source code.

Also Read :   Configure SSH on cpanel and cpanel with connect via putty authorise ssh key via cli

Make a new PHP file and name Like it as do_import.php as well as simple copy and paste the below source codes on the file.

<?php
 
	session_start();
	//Database connection
	$db_con = new mysqli('localhost', 'USerName', 'Pas85@#$5', 'live24u');
 
	if(isset($_POST['import'])){
		
		if(!empty($_FILES['file']['name'])){
			$livefName = $_FILES['file']['tmp_name'];
			$info_file = pathinfo($_FILES['file']['name']);
 
			//check file extension
			if(strtolower($info_file['extension']) == 'csv'){
				//check if file contains data
				if($_FILES['file']['size'] > 0){
 
					$file = fopen($livefName, 'r');
 
					while(($liveDta = fgetcsv($file, 1000, ',')) !== FALSE){
						$lstsql = "INSERT INTO students (studFname, studLname, address) VALUES ('".$liveDta[0]."', '".$liveDta[1]."', '".$liveDta[2]."')";
						$query = $db_con->query($lstsql);
 
						if($query){
							$_SESSION['logMsg'] = "Good Luck, Data imported successfully";
						}
						else{
							$_SESSION['logMsg'] = "Sorry, Cannot import data. Something went wrong";
						}
					}
					header('location: index.php');
 
				}
				else{
					$_SESSION['logMsg'] = "Sorry, File contains empty data";
					header('location: index.php');
				}
			}
			else{
				$_SESSION['logMsg'] = "Please Step By step upload CSV files only";
				header('location: index.php');
			}
		}
		else{
			$_SESSION['logMsg'] = "Sorry, File empty";
			header('location: index.php');
		}
 
	}
 
	else{
		$_SESSION['logMsg'] = "Step By step Please import a file first";
		header('location: index.php');
	} 
 
?>

And last, if you have any format doc file an excel data file instead of csv data file, so simple save the file as a create ne file like as .csv file.

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

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

I hope you have Got What is How To Import CSV File Into MySQL Using PHP and MySQLi And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.