Q:
How do I import Excel data into MySQL database using PHP?
belongs to collection: PHP Programming Exercises
PHP Programming Exercises
- Write a php program to compare between things that are not integers
- Write a division table program in PHP using for loop
- Write a program in PHP to print prime numbers between 1 and 100
- Write a php program to print numbers from 10 to 1 using the recursion function
- Write a php program to store the username in cookie and check whether the user have successfully login or not
- Write a php program to convert the given string into an array
- Write a php program to loop over the json data
- Write a program in PHP to remove all html tags except paragraph and italics tags
- Write a program to loop through an associative array using foreach() or with each()
- Write a php program to differentiate between fgets, fgetss and fgetcsv
- There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item
- Write a php program to set session on successful login
- Write a program in PHP to read from directory
- PHP create image from text and save
- How to get data from XML file in PHP
- PHP Create Word Document from HTML
- How to check whether a year is a leap year or not in PHP
- Fibonacci Series Program in PHP
- How to generate QR Code in PHP
- How does PHP store data in cache?
- How to detect a mobile device using PHP?
- How to send HTML form data to email using PHP?
- How to get location from IP address using PHP?
- How to lock a file using PHP?
- How to import a CSV file into MySQL using PHP
- How to fetch data from database in PHP and display in PDF
- How to insert image in database using PHP
- How to remove last character from string using PHP?
- Write a PHP program to reverse a string without predefined function
- Write a PHP program to calculate percentage of total
- How to sanitize input for MySQL using PHP?
- Write a program to calculate electricity bill in PHP
- How to send email with SMTP in PHP?
- How to Send Text Messages With PHP?
- How to convert stdClass object to Array in PHP?
- How do I import Excel data into MySQL database using PHP?
- How can I post a form without refreshing the page?
- How to sort table columns with PHP and MySQL?
- How to get current directory, filename and code line number in PHP
These are the steps to import Excel data to MySQL using PHPExcel -
Install PHPExcel with Composer
First, we need to install the PHPExcel library to read excel data. You can either download this from Github or install it using Composer. If your system is not installed composer, then first download the latest composer version from its official website -
https://getcomposer.org/download/
You can check a successful installation using the following command on cmd -
Now, go to your project directory on the command prompt and run the following command to install the PHPExcel library.
E:\wamp\www\exceltomysql>composer require phpoffice/phpexcel
After this, you have noticed that Composer has downloaded all libraries under the 'vendor' directory of your project root. Here is the file structure of this project -
Excel File
Suppose we have the following excel file containing school program participants data -
Database Script (db.php)
Here, we have written the database connection code, make sure to replace 'hostname', 'username', 'password' and 'database' with your database credentials and name.
<?php $hostname = "localhost"; $db = "school"; $password = ""; $user = "root"; $mysqli = new mysqli($hostname, $user, $password, $db); ?>
If we want to store or import excel data into the MySQL database, we need a database table. So, we have a MySQL database 'school' and in it we have to create an 'participants' table with the given columns.
CREATE TABLE IF NOT EXISTS `participants` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `rollno` int(11) NOT NULL, `name` varchar(100) NOT NULL, `age` int(11) NOT NULL, `program` varchar(100) NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
Create Upload File Template
To create a file uploader, we have created a simple HTML file upload form 'index.php'. If a form contains any file type input field, then we have to add an attribute 'encrypt' with a value 'multipart/form-data'.
Parse Excel and Insert into Database (uploadexcel.php)
In this PHP code, we have validated the uploaded file type and then included the PHPExcel library files and database configuration file (db.php). At each logical code, we have validated the execution. It returns an error message on failure. Further, PHPExcel parses the excel file, reads the data and stores it in an array. We have looped over this array to insert data into the database and show the response to the web browser.
On successful execution, the data will be inserted in the database as in the given screenshot -

need an explanation for this answer? contact us directly to get an explanation for this answer