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




Demo With Download Import a CSV File with PHP & MySQL Script


Your file has been imported.

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

Please UPLOAD / Choose your file:

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

Leave a Comment