• We just launched and are currently in beta. Join us as we build and grow the community.

How to Import Excel/CSV file to MySQL Database using PHP

billyhuynh

E-Commerce Pro
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
125
Likes
36
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial im going to show you how to create n application that can be used to Import CSV/Excel file using PHP. We will create a simple PHP web application that has a form for our import feature and table to display our imported data to the database.

*Upgrade Info

The tutorial source code was originally written in PHP MySQL and I have updated it into PHP MySQLi.

To start with this project, please make sure that you have already installed a local web server that can run PHP script and MySQL Database such as "XAMPP" and "WAMP". Then also make sure that the "Apache" and "MySQL" is started (for XAMPP/WAMP, open the XAMPP/WAMP Control Panel and start the Apache and MySQL).

Now, we will create a Database in your PHPMyAdmin named “studentdb” then execute this SQL query to create a new table called "subject".


  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `subject`

    (
  2. `SUBJ_ID`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `SUBJ_CODE`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `SUBJ_DESCRIPTION`

    VARCHAR

    (

    255

    )

    NOT

    NULL

    ,
  5. `UNIT`

    INT

    (

    2

    )

    NOT

    NULL

    ,
  6. `PRE_REQUISITE`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    DEFAULT

    'None'

    ,
  7. `COURSE_ID`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  8. `AY`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  9. `SEMESTER`

    VARCHAR

    (

    20

    )

    NOT

    NULL

    ,
  10. PRIMARY

    KEY

    (

    `SUBJ_ID`

    )
  11. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1 AUTO_INCREMENT

    =

    500

    ;

Then inside you htdocs or document root folder create a new folder and name it as “excel”. Next, we need to create a PHP file naming “db.php” that will hold our database connection and here’s the following code:

  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    or die

    (

    "Could not connect"

    )

    ;
  3. mysqli_select_db

    (

    $conn

    ,

    "studentdb"

    )

    or die

    (

    "could not connect database"

    )

    ;
  4. ?>

Next, create another PHP file named “index.php”,and this PHP file will be the first page that will load to our browser when we will access/browse the “excel” folder from our web directory. And this file will load all the list of subjects if the subject table is not empty well as this page will allow the user to import the CSV/Excel file and upload the data to MySQL Database.
The page Interface will be look like the image below.

i1.png


And here’s the code for the “index.php” file:

  1. <!DOCTYPE html>
  2. <?php
  3. include

    'db.php'

    ;

  4. ?>
  5. <html lang="en">
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Import Excel To MySQL Database Using PHP </title>
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <meta name="description" content="Import Excel File To MySQL Database Using php">

  11. <link rel="stylesheet" href="css/bootstrap.min.css">
  12. <link rel="stylesheet" href="css/bootstrap-responsive.min.css">
  13. <link rel="stylesheet" href="css/bootstrap-custom.css">


  14. </head>
  15. <body>

  16. <!-- Navbar
  17. ================================================== -->

  18. <div class="navbar navbar-inverse navbar-fixed-top">
  19. <div class="navbar-inner">
  20. <div class="container">
  21. <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  22. <span class="icon-bar"></span>
  23. <span class="icon-bar"></span>
  24. <span class="icon-bar"></span>
  25. </a>
  26. <a class="brand" href="#">Import Excel To MySQL Database Using PHP</a>

  27. </div>
  28. </div>
  29. </div>

  30. <div id="wrap">
  31. <div class="container">
  32. <div class="row">
  33. <div class="span3 hidden-phone"></div>
  34. <div class="span6" id="form-login">
  35. <form class="form-horizontal well" action="import.php" method="post" name="upload_excel" enctype="multipart/form-data">
  36. <fieldset>
  37. <legend>Import CSV/Excel file</legend>
  38. <div class="control-group">
  39. <div class="control-label">
  40. <label>CSV/Excel File:</label>
  41. </div>
  42. <div class="controls">
  43. <input type="file" name="file" id="file" class="input-large">
  44. </div>
  45. </div>

  46. <div class="control-group">
  47. <div class="controls">
  48. <button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Upload</button>
  49. </div>
  50. </div>
  51. </fieldset>
  52. </form>
  53. </div>
  54. <div class="span3 hidden-phone"></div>
  55. </div>

  56. <table class="table table-bordered">
  57. <thead>
  58. <tr>
  59. <th>ID</th>
  60. <th>Subject</th>
  61. <th>Description</th>
  62. <th>Unit</th>
  63. <th>Semester</th>


  64. </tr>
  65. </thead>
  66. <?php
  67. $SQLSELECT

    =

    "SELECT * FROM subject "

    ;
  68. $result_set

    =

    mysqli_query

    (

    $conn

    ,

    $SQLSELECT

    )

    ;
  69. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $result_set

    )

    )
  70. {
  71. ?>

  72. <tr>
  73. <td><?php

    echo

    $row

    [

    'SUBJ_ID'

    ]

    ;

    ?>

    </td>
  74. <td><?php

    echo

    $row

    [

    'SUBJ_CODE'

    ]

    ;

    ?>

    </td>
  75. <td><?php

    echo

    $row

    [

    'SUBJ_DESCRIPTION'

    ]

    ;

    ?>

    </td>
  76. <td><?php

    echo

    $row

    [

    'UNIT'

    ]

    ;

    ?>

    </td>
  77. <td><?php

    echo

    $row

    [

    'SEMESTER'

    ]

    ;

    ?>

    </td>


  78. </tr>
  79. <?php
  80. }
  81. ?>
  82. </table>
  83. </div>

  84. </div>

  85. </body>
  86. </html>

Next, we’re going to create another PHP file naming “import.php” that we will use to process the data from CSV/Excel to MySQL Database. Here’s the following code:

  1. <?php
  2. include

    'db.php'

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    "Import"

    ]

    )

    )

    {


  4. echo

    $filename

    =

    $_FILES

    [

    "file"

    ]

    [

    "tmp_name"

    ]

    ;


  5. if

    (

    $_FILES

    [

    "file"

    ]

    [

    "size"

    ]

    >

    0

    )
  6. {

  7. $file

    =

    fopen

    (

    $filename

    ,

    "r"

    )

    ;
  8. while

    (

    (

    $emapData

    =

    fgetcsv

    (

    $file

    ,

    10000

    ,

    ","

    )

    )

    !==

    FALSE

    )
  9. {

  10. //It wiil insert a row to our subject table from our csv file`
  11. $sql

    =

    "INSERT into subject (`SUBJ_CODE`, `SUBJ_DESCRIPTION`, `UNIT`, `PRE_REQUISITE`,COURSE_ID, `AY`, `SEMESTER`)
  12. values('$emapData[1]

    ','$emapData[2]

    ','$emapData[3]

    ','$emapData[4]

    ','$emapData[5]

    ','$emapData[6]

    ','$emapData[7]

    ')"

    ;
  13. //we are using mysql_query function. it returns a resource on true else False on error
  14. $result

    =

    mysqli_query

    (

    $conn

    ,

    $sql

    )

    ;
  15. if

    (

    !

    $result

    )
  16. {
  17. echo

    "<script type=\"

    text/javascript\"

    >
  18. alert(\"

    Invalid File:Please Upload CSV File.\"

    );
  19. window.location = \"

    index.php\"
  20. </script>"

    ;

  21. }

  22. }
  23. fclose

    (

    $file

    )

    ;
  24. //throws a message if data successfully imported to mysql database from excel file
  25. echo

    "<script type=\"

    text/javascript\"

    >
  26. alert(\"

    CSV File has been successfully Imported.\"

    );
  27. window.location = \"

    index.php\"
  28. </script>"

    ;



  29. //close of connection
  30. mysqli_close

    (

    $conn

    )

    ;



  31. }
  32. }
  33. ?>

After this, you can try it on your own, and I have attached with this sourcecode the example CSV/Excel file used for this tutorial, or you can also create a new CSV file to Import using below the below column orders.

ID

Subject Code

Subject Description

Unit

Pre Requisite

Course ID

AY (Academic Year)

Semester

DEMO

That's it! That would be the end of our tutorial on Importing CSV/Excel File data to MySQL Database using PHP. By the way in this application I am using the Twitter bootstrap framework, you can download and use it like in this application.

If you want to see some new Source Code or Application and Tutorials Just click here.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

451,993

323,299

323,308

Top