Importing CSV File to MySQL using PHP with Bootstrap Templates

If you are looking for Importing CSV File to MySQL using PHP with Bootstrap Templates then you are at the right place. We all know that CSV file format used to import or to export the table data. Every line from the CSV file format indicates one field for the data table in the database. For inserting data one by one to the MySQL Table, it's very time consuming to do that, instead of this procedure you can use the importing file using CSV File Format to insert a bundle of data just in one process in your web page. In this tutorial, we are going to show or to learn on how to import data using CSV File Format to MySQL Database Table using the PHP Programming Language. Let's start to create our database table.
  1. Open the PHPMyAdmin.
  2. Create a database.
  3. After creating a database. Kindly click the SQL below, copy and paste to your PHPMyAdmin to create a table.
  1. CREATE TABLE `user`
  2.   (
  3.      `user_id`    INT(11) NOT NULL,
  4.      `user_name`  VARCHAR(100) NOT NULL,
  5.      `first_name` VARCHAR(100) NOT NULL,
  6.      `last_name`  VARCHAR(100) NOT NULL,
  7.      `date_added` DATETIME NOT NULL
  8.   )
  9. engine=innodb
  10. DEFAULT charset=latin1;
Database Connection This simple PHP query helps us to connect and select the database. Save it as "database.php".
  1. <?php
  2. mysql_select_db('import_file', mysql_connect('localhost', 'root', '')) or die(mysql_error());
  3. ?>
Create Markup We have to create simple markup for the importing data. Take a look the source code below and study.
  1. <form action="import_query.php" method="post" name="upload_excel" enctype="multipart/form-data">
  2.    <div>
  3.       <div class="col-xs-6">
  4.          <label>Import CSV/Excel File:</label>
  5.          <input type="file" class="form-control" multiple name="filename" id="filename">
  6.          <br />
  7.          <button type="submit" id="submit" class="btn btn-primary" name="submit" data-loading-text="Loading...">Upload</button>
  8.       </div>
  9.    </div>
  10. </form>
To view the data from the database, we have simple source code that we construct to display to your web page. Copy and paste it to the BODY tag of your web page.
  1.    <tr>
  2.       <th>First Name</th>
  3.       <th>Last Name</th>
  4.       <th>User Name</th>
  5.    </tr>
  6.    <?php
  7.      include ('database.php');
  8.      $result= mysql_query("select * from user order by user_id ASC") or die (mysql_error());
  9.      while ($row= mysql_fetch_array ($result) ){
  10.      $id=$row['user_id'];
  11.      ?>
  12.    <tr>
  13.       <td><?php echo $row['user_name']; ?></td>
  14.       <td><?php echo $row['first_name']; ?></td>
  15.       <td><?php echo $row['last_name']; ?></td>
  16.    </tr>
  17.    <?php } ?>
PHP INSERT Statement This is the simple query for Importing CSV File to the MySQL Database. Take a look the source code below and try to study it.
  1. <?php
  2.  
  3. if (isset($_POST['submit']))
  4.         {
  5.         include ('CSV_row_Database.php');
  6.  
  7.         // Import uploaded file to CSV_row_Database
  8.  
  9.         $file_CSV = fopen($_FILES['filename']['tmp_name'], "r");
  10.         while (($CSV_row_Data = fgetcsv($file_CSV, 1000, ",")) !== FALSE)
  11.                 {
  12.                 mysql_query("INSERT into user (user_name, first_name, last_name, date_added)
  13. values('$CSV_row_Data[0]', '$CSV_row_Data[1]', '$CSV_row_Data[2]', NOW())");
  14.                 }
  15.  
  16.         fclose($file_CSV);
  17.  
  18.         // print "Import done";
  19.  
  20.         echo "<script type='text/javascript'>alert('Successfully Imported a CSV File for User!');</script>";
  21.         echo "<script>document.location='index.php'</script>";
  22.  
  23.         // view upload form
  24.  
  25.         }
  26.  
  27. ?>

Output

Result For the beginners who really want to learn how to codes in PHP, this tutorial will help you to your future projects and you can implement the Importing Data Function in your web projects.

Add new comment