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.

Read Also:  Import Excel File Data In Database Using PHP

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.

		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`)

Step 2: Connection of MYSQL database using 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.

Read Also:  how to upload excel file in php?


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


// 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
    } 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-->
<title>Demo With Download Import a CSV File with PHP & MySQL Script</title>
	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" />

<!--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. Read Also:  WooCommerce Get Product Prices
  2. Read Also:  UI Grid Filtering using Angular Example
  3. Read Also:  For Loop inside React JSX components
  4. Read Also:  laravel redirect new tab
  5. Read Also:  How to add Ajax loader image in PHP?
  6. Read Also:  Laravel 6.2 Disable CSRF Protection on Specific Routes
  7. Read Also:  Polymorphism in PHP Object-oriented programming Examples
  8. Read Also:  Upload video files Images using PHP mysql
  9. Read Also:  cURL Requests with Laravel 6 using ixudra/curl
  10. Read Also:  jQuery Ajax QR Code Generator PHP script Download