Skip to main content

PHP - Import CSV File Into MySQLi Database

Screenshot
Body
In this tutorial we will create a Import CSV File Into MySQLi Database using PHP. This code will upload csv file as an array data to the database server when user click the upload button. The code use MySQLi INSERT query to store csv array object to the database server by using fgetcsv() in order to break down the array element. This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_csv, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_csv");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7.  
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class = "text-primary">PHP - Import CSV File Into MySQLi Database</h3>
  17. <hr style = "border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4" style='overflow:hidden;'>
  19. <form action="upload.php" class="form-inline" method="POST" enctype="multipart/form-data">
  20. <div class="form-group">
  21. <label>Upload CSV file here:</label>
  22. <br />
  23. <input type="file" name="file"/>
  24. <br />
  25. <center><button name="upload" class="btn btn-primary"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
  26. </div>
  27. </form>
  28. </div>
  29. <div class="col-md-8">
  30. <table class="table table-bordered">
  31. <thead class="alert-info">
  32. <tr>
  33. <th>Firstname</th>
  34. <th>Lastname</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <?php
  39. require'conn.php';
  40. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  41. while($fetch=mysqli_fetch_array($query)){
  42. ?>
  43. <tr>
  44. <td><?php echo $fetch['firstname']?></td>
  45. <td><?php echo $fetch['lastname']?></td>
  46. </tr>
  47. <?php
  48. }
  49. ?>
  50. </tbody>
  51. </table>
  52. </div>
  53. </div>
  54. </body>
  55. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload csv file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as upload.php
  1. <?php
  2. require 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. if($_FILES['file']['name']){
  6. $filename = explode(".", $_FILES['file']['name']);
  7. $ext=end($filename);
  8.  
  9. if($ext=="csv"){
  10. $handler=fopen($_FILES['file']['tmp_name'], "r");
  11. while($data=fgetcsv($handler)){
  12. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$data[0]', '$data[1]')") or die(mysqli_error());
  13. }
  14.  
  15. header('location: index.php');
  16. }else{
  17. echo"<script>alert('Only csv file is allowed to be upload!')</script>";
  18. echo"<script>window.location='index.php'</script>";
  19. }
  20. }
  21. }
  22. ?>
There you have it we successfully created Import CSV File Into MySQLi Database using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment