PHP MYSQLi Import Data From Excel to MYSQL

PHP MYSQLi Import Data From Excel to MYSQL

Today, We want to share with you PHP MYSQLi Import Data From Excel to MYSQL.In this post we will show you , hear for Import Excel File Data in MySQL Database using PHP we will give you demo and example for implement.In this post, we will learn about PHP MySQLi- import excel file into mysql database Tutorial with an example.

Create a table in your server

Goto localhost/phpmyadmin then crate a database like (Dtabase name = “phpdataimport”)

Then select your database and Go to SQL tab select there paset this code and create a table.

Step 1 : Create Table in MYSQL Database using PHP

  1. First of all create a database like..
    Database Name : pakainfo
  2. Open mysql : http://localhost/phpmyadmin/
  3. select your database(pakainfo) and select opetion SQL
    and then run this Script and create a databased.

		SET FOREIGN_KEY_CHECKS=0;
		DROP TABLE IF EXISTS `pakainfo`;
		CREATE TABLE `pakainfo` (
		`funny_id` int(11) NOT NULL auto_increment,
		`funny_first` varchar(255) character set latin1 default NULL,
		`funny_last` varchar(255) character set latin1 default NULL,
		`funny_email` varchar(255) character set latin1 default NULL,
		`funny_mobile` varchar(255) character set latin1 default NULL,
		PRIMARY KEY  (`funny_id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;
		

Step 2: Connection of MYSQL database using PHP

config.php

   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = '';
   $dbname = 'pakainfo';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }
	mysql_select_db($dbname) or die(mysql_error());

Step 3: Import CSV file in PHP to MYSQL

Now create a PHP file (Import.php)
Easly Import Data CSV to MYSQL database using PHP.

Import_data.php

 
//First connect to the database(MYSQL)
//$connect_pakainfo = mysql_connect("localhost","root","");
//mysql_select_db("pakainfo",$connect_pakainfo); //select the table(MYSQL)

or 

include("config.php");
// Condtion of csv file in PHP
if ($_FILES['csv_free']['size'] > 0) {

    //There get the csv file
    $file = $_FILES['csv_free']['tmp_name'];
    $handle = fopen($file,"r");
   
    //using loop through the csv_free file and insert into database
    do {
        if ($data_csv[0]) {
            mysql_query("INSERT INTO pakainfo (funny_first, funny_last, funny_email) VALUES
                (
                    '".addslashes($data_csv[0])."',
                    '".addslashes($data_csv[1])."',
					'".addslashes($data_csv[2])."',
                    '".addslashes($data_csv[3])."'
                )
            ");
        }
    } while ($data_csv = fgetcsv($handle,1000,",","'"));
 
    //redirect this url and show message
    header('Location: import.php?an=importpakainfo&success=1'); die;
}

Import a CSV File HTML PART START using PHP and MYSQL

<!--Import a CSV File HTML PART START-->
<html>
<head>
<title>Demo With Download Import a CSV File with PHP & MySQL Script</title>
</head>
<body>
<?php 
	if (!empty($_GET['success'])) 
	{ 
		echo "<b>Your file has been imported.</b>

"; 
	} //This is generic success notice message
?>

<form action="" method="post" enctype="multipart/form-data" name="ng_form1" id="ng_form1">
  Please UPLOAD / Choose your file: 
  <input name="csv_free" type="file" id="csv_free" />
  <input type="submit" name="Submit(Uploading)" value="Submit" />
</form>

</body>
</html>
<!--Import a CSV File HTML PART END-->

Step 4: structure of Excel/CSV Format Example

Beverly	    Moreno	Tucker  9898898987
Benjamin	Lewis	Lopez	9856898984	
Donald	    Russell	Bryant	9857566665
Chris	    Boyd	Jenkins 7844545456
Scott	    Wright	West	9825633355

Import Data From Excel to MYSQL using PHP Code
Import Data From Excel to MYSQL using PHP Code

Related FAQ

Here are some more FAQ related to this Article:

  1.   Setup Laravel 6 Login Authentication
  2.   Laravel Ajax Bootstrap Toggle switch Update MySQL
  3.   Laravel 6 get Session ID Easily
  4.   echo and print difference in php
  5.   How to Get the Value of Selected Option in a Combo Box Using jQuery?
  6.   Laravel 7 jQuery Ajax Form Validation Example
  7.   How to Check Whether a Variable is Set or Not in laravel blade?
  8.   PHP Conditional Control Statements with Example
  9.   How to get Ip Address in Laravel 5.8?
  10.   PHP Force File Download HTML button code