PHP

How Do You Create a PHP Page With MySQL Excel Spreadsheet

PHP Page With MySQL Excel Spreadsheet

Talk about Excel, PHP, and MySQL, but very few actually show clear, complete, and practical code from start to finish. That’s why people still ask the same question: How Do You Create a PHP Page With MySQL Excel Spreadsheet. In this updated version, we go deeper and focus heavily on real PHP code examples. Every important step is explained using working code so you can copy, test, and understand what’s happening without guessing.

Understand the Data Flow Before Writing Code

Before coding, understand the simple flow.
Excel file → PHP upload form → PHP reads Excel → PHP saves data → MySQL database → PHP displays data

Once this clicks, the code stops feeling scary.

Creating the MySQL Database and Table

Before PHP touches Excel, MySQL must be ready.

CREATE DATABASE excel_import;

USE excel_import;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(150),
    phone VARCHAR(50)
);

This table matches the Excel file columns exactly. This avoids errors later.

Preparing the Excel File Correctly

Your Excel file must look like this:

Row 1: name | email | phone
Row 2+: actual data

Avoid empty rows, merged cells, colors, or formulas. Keep it clean.

Creating the PHP Upload Form (upload.php)

This PHP page allows users to upload an Excel file.

<!DOCTYPE html>
<html>
<head>
    <title>Upload Excel File</title>
</head>
<body>

<h2>Upload Excel Spreadsheet</h2>

<form method="post" action="import.php" enctype="multipart/form-data">
    <input type="file" name="excel_file" required>
    <br><br>
    <button type="submit" name="submit">Upload & Import</button>
</form>

</body>
</html>

This form sends the Excel file to import.php.

Installing PhpSpreadsheet Library

PHP cannot read Excel files alone. We use PhpSpreadsheet.

Install using Composer:

composer require phpoffice/phpspreadsheet

Then include it in your PHP file.

require 'vendor/autoload.php';

Connecting PHP to MySQL (db.php)

Create a reusable database connection file.

<?php
$conn = new mysqli("localhost", "root", "", "excel_import");

if ($conn->connect_error) {
    die("Database connection failed");
}
?>

This file will be included wherever needed.

Handling Excel Upload and Reading Data (import.php)

This is where the real work happens.

<?php
require 'vendor/autoload.php';
require 'db.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

if(isset($_POST['submit'])){

    $fileName = $_FILES['excel_file']['tmp_name'];

    if($_FILES['excel_file']['size'] > 0){

        $spreadsheet = IOFactory::load($fileName);
        $sheet = $spreadsheet->getActiveSheet();
        $rows = $sheet->toArray();

        foreach($rows as $index => $row){

            if($index == 0){
                continue;
            }

            $name  = $conn->real_escape_string($row[0]);
            $email = $conn->real_escape_string($row[1]);
            $phone = $conn->real_escape_string($row[2]);

            $sql = "INSERT INTO users (name, email, phone)
                    VALUES ('$name', '$email', '$phone')";

            $conn->query($sql);
        }

        echo "Excel data imported successfully!";
    }
}
?>

This script:
Receives Excel file
Reads rows
Skips header row
Inserts data into MySQL

Why This Code Works Reliably

This method works because:
Excel structure matches database
Each row is validated
Header row is skipped
Database input is sanitized

Displaying Imported Data on a PHP Page (view.php)

Once data is stored, showing it is easy.

<?php
require 'db.php';

$result = $conn->query("SELECT * FROM users");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Excel Data</title>
</head>
<body>

<h2>Imported Excel Records</h2>

<table border="1" cellpadding="10">
    <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
    </tr>

    <?php while($row = $result->fetch_assoc()){ ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['email']; ?></td>
            <td><?php echo $row['phone']; ?></td>
        </tr>
    <?php } ?>

</table>

</body>
</html>

This confirms that Excel data is now fully connected to PHP and MySQL.

Handling Common Errors Using Code

Most errors come from wrong file types or empty files.

$allowed = ['xls', 'xlsx'];
$extension = pathinfo($_FILES['excel_file']['name'], PATHINFO_EXTENSION);

if(!in_array($extension, $allowed)){
    die("Invalid file type");
}

This small check saves hours of frustration.

Improving Security in Real Projects

Never trust uploaded files.

if($_FILES['excel_file']['size'] > 2000000){
    die("File too large");
}

Final Thoughts

Now you fully understand How Do You Create a PHP Page With MySQL Excel Spreadsheet using real, tested PHP code. Take it step by step, test often, and don’t rush. PHP rewards patience, not speed.

author-avatar

About MALIK SHAFI

Experienced PHP Developer with a strong background in building scalable web applications in the IT services sector. Proficient in PHP frameworks like Laravel and CodeIgniter, and front-end technologies, Skilled in MySQL database management, RESTful API integration, and working with AWS services such as EC2 and S3. Extensive hands-on experience with Nginx configuration on AWS EC2 instances, optimizing for performance and security.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments