Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // UPLOAD A CSV FILE USING PHP TO MySQL
- // AND EXCLUDE HEADER ROW
- // CODE IS TAKEN FROM:
- // http://www.johnboy.com/blog/tutorial-import-a-csv-file-using-php-and-mysql
- // WITH ASSISTANCE OF:
- // http://board.phpbuilder.com/showthread.php?10376604-PHP-import-CSV-and-skip-headers
- // WITH PERSONAL ASSISTANCE OF:
- // Ms. Tahmina Aktar Nishi
- ?>
- <?php
- // CONNECT TO THE DATABASE
- $connect = mysql_connect( "localhost","root","" );
- mysql_select_db( "csv", $connect ); //select the table
- // CSV IMPORT CODES WILL BE HERE
- if( isset($_POST['Submit'] ) ) {
- if( $_FILES['csv']['size'] > 0 ) {
- //get the csv file
- $file = $_FILES[csv][tmp_name];
- $handle = fopen($file,"r"); // read the file
- ?>
- <?php
- // make the loop and do upload
- $find_header = 0; // a simple counter
- while( $data = fgetcsv( $handle,6000,",","'") ) {
- $find_header++; //update counter
- if( $find_header > 1 ) {
- $sql = mysql_query( "INSERT INTO table_name (
- post_id,
- name,
- designation
- )
- VALUES
- (
- '".$data[0] ."',
- '".$data[1] ."',
- '".$data[2] ."'
- )
- ");
- }
- } // endwhile
- //redirect
- header('Location: index.php?success=1'); die;
- } // endif( $_FILES['csv']['size'] > 0 )
- } // endif( isset($_POST['Submit'] ) )
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>CSV UPLOAD</title>
- </head>
- <body>
- <?php
- if ( !empty( $_GET['success'] ) ) {
- echo "<b style=\"color: green;\">Your file has been imported successfully.</b><br/><br/>"; //generic success notice
- }
- ?>
- <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
- Choose your file: <br />
- <input name="csv" type="file" id="csv" />
- <input type="submit" name="Submit" value="Submit" />
- </form>
- </body>
- </html>
- --------------------------------------------------------------------------------------------------
- AND THE MySQL QUERY IS:
- CREATE TABLE IF NOT EXISTS `table_name` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `post_id` int(3) NOT NULL,
- `name` varchar(100) NOT NULL,
- `designation` varchar(50) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement