Import CSV File Data into MySQL Database using PHP

Import CSV file data into mysql database using php; In this tutorial; we will learn how to how to import CSV file data into MySQL database using PHP script.

How to Import CSV File Data into MySQL Database using PHP

Follow the below steps to import CSV file data into MySQL using PHP script or code:

  • Step 1 – Create PHP Project
  • Step 2 – Create Table in Database
  • Step 3 – Create a Database Connection File
  • Step 4 – Create HTML Form To Upload CSV File
  • Step 5 – Create PHP File To Import Csv File Data Into Database

Step 1 – Create PHP Project

First of all; visit web server directory and create a php directory; which name demo.

Step 2 – Create Table in Database

Create table into your database; so visit PHPMyAdmin and create a table name users with the following fields: name, email, mobile.

CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
 `phone` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 `status` enum('Active','Inactive') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Active',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Step 3 – Create a Database Connection File

Create a file name db.php and update the below code into your file.

Step 4 – Create HTML Form To Upload CSV File

Create a simple HTML upload csv file form and add the following code into index.php file:





  
  
  

  
  

  Import CSV File into MySQL using PHP



  

Step 5 – Create PHP File To Import Csv File Data Into Database

Create one file name upload.php; which is used to read csv file and insert all csv file data into MySQL database. So add the following code into upload.php file:

num_rows > 0)
                {
                    mysqli_query($conn, "UPDATE users SET name = '" . $name . "', phone = '" . $phone . "', status = '" . $status . "', created_at = NOW() WHERE email = '" . $email . "'");
                }
                else
                {
                     mysqli_query($conn, "INSERT INTO users (name, email, phone, created_at, updated_at, status) VALUES ('" . $name . "', '" . $email . "', '" . $phone . "', NOW(), NOW(), '" . $status . "')");

                }
            }

            // Close opened CSV file
            fclose($csvFile);

            header("Location: index.php");
        
    }
    else
    {
        echo "Please select valid file";
    }
}



Conclusion

PHP import data into MySQL database; Through this tutorial, we have learned how to upload csv file and import/insert data into MySQL database using PHP.

Recommended PHP Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

Images mentioned above related to Bootstrap,MySQL,PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is senior ICT professional who talks about #it, #cloud, #servers, #software, and #innovation. Rabins is also the first initiator of Digital Nepal. Facebook: rabinsxp Instagram: rabinsxp

Leave a Reply

Your email address will not be published. Required fields are marked *