All TalkersCode Topics

Follow TalkersCode On Social Media

Read Csv File In PHP And Insert Into MySQL

Last Updated : Jul 1, 2023

Read Csv File In PHP And Insert Into MySQL

In this article we will show you the solution of read csv file in PHP and insert into MySQL, in this tutorial to read the CSV file use used a super global variable $_FILES in php.

$_FILES: this super global variable is an associative array uploaded through the HTTP POST method.

We also used fopen() and fgetcsv() functions here to open and get the CSV file.

Step By Step Guide On Read CSV File In PHP And Insert Into Mysql :-

In the example below, we read CSV file in php and insert it into MySQL.

<?php
$con = mysqli_connect("localhost", "root", "", "csv") ;
if(isset($_POST["import"])) {
    $fileName = $_FILES["file"]["tmp_name"] ;
    if($_FILES["file"]["size"]>0) {
        $file = fopen($fileName, "r") ;
        while(($column = fgetcsv($file,100,",")) !== FALSE) {
                $sqlInsert = "Insert into data (name,type) values ('".$column[0]."', '".column[1]."')" ;
                $result = mysqli_query($con, $sqlInsert) ;
                if(!empty($result)) {
                    echo "CSV data Imported into the database" ;
                } else {
                    echo "Problem to importing CSV" ;
                }
        }
    }
}
?>
<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> read csv file in php and insert into mysql </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> read csv file in php and insert into mysql </h2>
    <form action="" method="post" name="uploadCSV" enctype="multipart/form-data" class="form-horizontal">
        <div>
            <label> Choose CSV file </label>
            <input type="file" name="file" accept=".csv">
            <button type="submit" name="import"> Import </button>
        </div>
        </form>
        <?php
        //display uploaded data
        $sqlSelect = "SELECT * from data" ;
        $result = mysqli_connect($con, $sqlSelect) ;
        if(mysqli_num_rows($result) > 0) {
            ?>
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Country</th>
                    </tr>
                </thead>
                <?php
                while($row = mysqli_fetch_array($result)) {
                ?>
                <tbody>
                    <tr><?php echo $row['Name'] ?></tr>
                    <tr><?php echo $row['Email'] ?></tr>
                    <tr><?php echo $row['Country'] ?></tr>
                </tbody>
                <?php
                }
                ?>
            </table>
        <?php
        }
        ?>
</body>
</html>
  1. We write <?php tag to write PHP within it.
  2. For Connecting with the database, using mysqli_connect() with the server name, hostname and password
  3. $fileName with super global variable $_FILES
  4. Again create an If statement for if the file size is greater than 0 then fopen() to open the file.
  5. Using while loop with column with fgetcsv() is not FALSE then $sqlInsert and $results with mysqli_connect()
  6. If the result is now empty then use echo to display the success message or else display the error message
  7. Now using an if statement with $_POST method and button name ‘import’
  8. ?> to close the php code.
  9. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  10. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  11. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  12. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  13. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  14. <h1> tag used to add heading here and also adding the inline CSS here.
  15. Creating e form with method Post, name uploadCSV.
  16. Creating a <div> with <label> of Choose CSV file to upload the CSV file
  17. <input> with type file, name file and accept only .csv file and also created a <button> for submit with name import
  18. To Display uploaded data create a <?php file.
  19. $sqlSelect variable with SELECT
  20. $result with mysqli_connect() with $con and $sqlSelect
  21. Now create an if statement for if mysqli_num_rows($result) is greater than 0 then create a table with <table> tag.
  22. <thead> and<tr> and <th> to add table heading of Name,Email and Country
  23. Creating another php tag with while loop with $row as mysqli_fetch_array() for $result
  24. To display the table data we used <td> with inline php echo $row[ ]

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to read csv file in php and insert into mysql using php.

I hope this article on read csv file in PHP and insert into MySQL helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials