Read Csv File In PHP And Insert Into MySQL
Last Updated : Mar 11, 2024
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>
- We write <?php tag to write PHP within it.
- For Connecting with the database, using mysqli_connect() with the server name, hostname and password
- $fileName with super global variable $_FILES
- Again create an If statement for if the file size is greater than 0 then fopen() to open the file.
- Using while loop with column with fgetcsv() is not FALSE then $sqlInsert and $results with mysqli_connect()
- If the result is now empty then use echo to display the success message or else display the error message
- Now using an if statement with $_POST method and button name ‘import’
- ?> to close the php code.
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Creating e form with method Post, name uploadCSV.
- Creating a <div> with <label> of Choose CSV file to upload the CSV file
- <input> with type file, name file and accept only .csv file and also created a <button> for submit with name import
- To Display uploaded data create a <?php file.
- $sqlSelect variable with SELECT
- $result with mysqli_connect() with $con and $sqlSelect
- Now create an if statement for if mysqli_num_rows($result) is greater than 0 then create a table with <table> tag.
- <thead> and<tr> and <th> to add table heading of Name,Email and Country
- Creating another php tag with while loop with $row as mysqli_fetch_array() for $result
- 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.