How to Upload Excel File in Php

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

In this tutorial im going to testify yous how to create n application that can be used to Import CSV/Excel file using PHP. Nosotros will create a simple PHP spider 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 lawmaking was originally written in PHP MySQL and I have updated information technology into PHP MySQLi.

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

Now, nosotros will create a Database in your PHPMyAdmin named "studentdb" and then execute this SQL query to create a new table called "bailiwick".

              
  1. CREATE Tabular array IF Non EXISTS `subject` (

  2. `SUBJ_ID` INT ( 11 ) NOT Cypher AUTO_INCREMENT ,

  3. `SUBJ_CODE` VARCHAR ( 30 ) Non NULL ,

  4. `SUBJ_DESCRIPTION` VARCHAR ( 255 ) NOT NULL ,

  5. `Unit of measurement` INT ( ii ) NOT NULL ,

  6. `PRE_REQUISITE` VARCHAR ( thirty ) Non NULL DEFAULT 'None' ,

  7. `COURSE_ID` INT ( 11 ) Non Zero ,

  8. `AY` VARCHAR ( thirty ) NOT Nil ,

  9. `SEMESTER` VARCHAR ( 20 ) NOT Aught ,

  10. Main 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 proper noun it as "excel". Next, nosotros need to create a PHP file naming "db.php" that will agree our database connexion and here'south the following lawmaking:

              
  1. <?php

  2. $conn = mysqli_connect ( "localhost" , "root" , "" ) or die ( "Could not connect" ) ;

  3. ?>

Next, create another PHP file named "index.php",and this PHP file volition 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 listing of subjects if the subject tabular array is non empty well as this page will permit the user to import the CSV/Excel file and upload the data to MySQL Database.
The page Interface will be look similar the paradigm below.

i1

And hither's the code for the "alphabetize.php" file:

              
  1. <!DOCTYPE html>

  2. <?php

  3. include 'db.php' ;

  4. ?>

  5. <html lang="en">

  6. <head>

  7. <meta charset="utf-viii">

  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="clarification" 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 form="navbar-inner">

  20. <div form="container">

  21. <a class="btn btn-navbar" data-toggle="collapse" information-target=".nav-collapse">

  22. <span class="icon-bar"></bridge>

  23. <span class="icon-bar"></span>

  24. <span class="icon-bar"></span>

  25. </a>

  26. <a class="make" 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 form="span3 hidden-phone"></div>

  34. <div class="span6" id="course-login">

  35. <form form="form-horizontal well" action="import.php" method="post" name="upload_excel" enctype="multipart/course-information">

  36. <fieldset>

  37. <legend>Import CSV/Excel file</legend>

  38. <div class="control-group">

  39. <div grade="command-characterization">

  40. <label>CSV/Excel File:</characterization>

  41. </div>

  42. <div class="controls">

  43. <input type="file" name="file" id="file" class="input-large">

  44. </div>

  45. </div>

  46. <div class="command-group">

  47. <div form="controls">

  48. <button type="submit" id="submit" name="Import" form="btn btn-master button-loading" data-loading-text="Loading...">Upload</push button>

  49. </div>

  50. </div>

  51. </fieldset>

  52. </grade>

  53. </div>

  54. <div class="span3 hidden-telephone"></div>

  55. </div>

  56. <tabular array grade="tabular array table-bordered">

  57. <thead>

  58. <tr>

  59. <th>ID</th>

  60. <thursday>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. {

  69. ?>

  70. <tr>

  71. <td><?php echo $row [ 'SUBJ_ID' ] ; ?></td>

  72. <td><?php echo $row [ 'SUBJ_CODE' ] ; ?></td>

  73. <td><?php echo $row [ 'SUBJ_DESCRIPTION' ] ; ?></td>

  74. <td><?php echo $row [ 'Unit of measurement' ] ; ?></td>

  75. <td><?php repeat $row [ 'SEMESTER' ] ; ?></td>

  76. </tr>

  77. <?php

  78. }

  79. ?>

  80. </table>

  81. </div>

  82. </div>

  83. </body>

  84. </html>

Next, we're going to create another PHP file naming "import.php" that nosotros will use to procedure the data from CSV/Excel to MySQL Database. Here'south 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 bailiwick table from our csv file`

  11. $sql = "INSERT into subject (`SUBJ_CODE`, `SUBJ_DESCRIPTION`, `Unit of measurement`, `PRE_REQUISITE`,COURSE_ID, `AY`, `SEMESTER`)

  12. values('$emapData[1]','$emapData[ii]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]')" ;

  13. //we are using mysql_query function. information technology returns a resource on truthful else Faux on fault

  14. if ( ! $result )

  15. {

  16. echo "<script type=\"text/javascript\">

  17. warning(\"Invalid File:Please Upload CSV File.\");

  18. window.location = \"index.php\"

  19. </script>" ;

  20. }

  21. }

  22. //throws a message if information successfully imported to mysql database from excel file

  23. echo "<script type=\"text/javascript\">

  24. alert(\"CSV File has been successfully Imported.\");

  25. window.location = \"index.php\"

  26. </script>" ;

  27. //shut of connection

  28. }

  29. }

  30. ?>

After this, you tin can endeavour it on your own, and I accept 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 cavalcade orders.

ID Subject Code Field of study Clarification Unit of measurement Pre Requisite Form ID AY (Academic Yr) Semester

DEMO

That'due south it! That would be the finish of our tutorial on Importing CSV/Excel File data to MySQL Database using PHP. By the mode in this application I am using the Twitter bootstrap framework, you lot tin can download and employ it like in this application.

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

Happy Coding :)

rossthershe.blogspot.com

Source: https://www.sourcecodester.com/tutorials/php/6989/how-import-excelcsv-file-mysql-database-using-php.html

Belum ada Komentar untuk "How to Upload Excel File in Php"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel