Advertisement
wzislam

Exclude Header from CSV using PHP

Jul 15th, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.44 KB | None | 0 0
  1. <?php
  2. // UPLOAD A CSV FILE USING PHP TO MySQL
  3. // AND EXCLUDE HEADER ROW
  4.  
  5. // CODE IS TAKEN FROM:
  6. // http://www.johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql
  7. // WITH ASSISTANCE OF:
  8. // http://board.phpbuilder.com/showthread.php?10376604-PHP-import-CSV-and-skip-headers
  9. // WITH PERSONAL ASSISTANCE OF:
  10. // Ms. Tahmina Aktar Nishi
  11. ?>
  12.  
  13. <?php
  14. // CONNECT TO THE DATABASE
  15. $connect = mysql_connect( "localhost","root","" );
  16. mysql_select_db( "csv", $connect ); //select the table
  17.  
  18.  
  19. // CSV IMPORT CODES WILL BE HERE
  20. if( isset($_POST['Submit'] ) ) {
  21.    
  22.     if( $_FILES['csv']['size'] > 0 ) {
  23.    
  24.         //get the csv file
  25.         $file = $_FILES[csv][tmp_name];
  26.         $handle = fopen($file,"r"); // read the file
  27.         ?>
  28.                
  29.         <?php
  30.         // make the loop and do upload
  31.        
  32.         $find_header = 0; // a simple counter
  33.        
  34.         while( $data = fgetcsv( $handle,6000,",","'") ) {
  35.             $find_header++; //update counter
  36.            
  37.             if( $find_header > 1 ) {
  38.                 $sql = mysql_query( "INSERT INTO table_name (
  39.                         post_id,
  40.                         name,
  41.                         designation
  42.                     )
  43.                     VALUES
  44.                     (
  45.                         '".$data[0] ."',
  46.                         '".$data[1] ."',
  47.                         '".$data[2] ."'
  48.                     )
  49.                 ");
  50.             }
  51.         } // endwhile
  52.            
  53.      //redirect
  54.      header('Location: index.php?success=1'); die;
  55.      
  56.     } // endif( $_FILES['csv']['size'] > 0 )
  57.    
  58. } // endif( isset($_POST['Submit'] ) )
  59. ?>
  60.  
  61.  
  62. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  63. <html xmlns="http://www.w3.org/1999/xhtml">
  64. <head>
  65.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  66.     <title>CSV UPLOAD</title>
  67. </head>
  68.  
  69. <body>
  70.  
  71. <?php
  72. if ( !empty( $_GET['success'] ) ) {
  73.     echo "<b style=\"color: green;\">Your file has been imported successfully.</b><br/><br/>"; //generic success notice
  74.     }
  75. ?>
  76.  
  77. <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  78.   Choose your file: <br />
  79.   <input name="csv" type="file" id="csv" />
  80.   <input type="submit" name="Submit" value="Submit" />
  81. </form>
  82.  
  83. </body>
  84. </html>
  85.  
  86.  
  87. --------------------------------------------------------------------------------------------------
  88.  
  89. AND THE MySQL QUERY IS:
  90.  
  91. CREATE TABLE IF NOT EXISTS `table_name` (
  92.   `id` int(11) NOT NULL AUTO_INCREMENT,
  93.   `post_id` int(3) NOT NULL,
  94.   `name` varchar(100) NOT NULL,
  95.   `designation` varchar(50) NOT NULL,
  96.   PRIMARY KEY (`id`)
  97. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement