www.webdeveloper.com
Results 1 to 3 of 3

Thread: Help Importing .csv data in specific rows only

  1. #1
    Join Date
    Nov 2012
    Posts
    2

    Help Importing .csv data in specific rows only

    im using this code php code to upload the rows

    PHP Code:
        <?php
        
    include "connect.php";
        
        if(isset(
    $_POST['submit']))

       {

         
    $filename=$_POST['filename'];

         
    $handle fopen("$filename""r");
         

         while ((
    $data fgetcsv($handle1000",")) !== FALSE)

         {

            
    $user_address mysql_real_escape_string($data[1]);

           
    $import="INSERT into Persons(Firstname,Lastname,Age,Lots) values('$data[0]','$data[1]','$data[2]','$data[3]')";

           
    mysql_query($import) or die(mysql_error());

         }

         
    fclose($handle);

         print 
    "Import done";

     

       }
    but the thing is the csv files have their row headers and was made in excel

    but this code upload all rows

    please i really need help.

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Try this script I made for you, it looks a bit more robust to me.

    If you want to skip the first record, just use fgetcsv() on the csv and don't store the row of values anywhere (just toss it out).

    PHP Code:
    <?php

    /* include database credentials and open mysql connection */
    include "connect.php";

    /**
     * a flag that decides if we should skip the first record in the CSV (column headings) or not
     * TRUE means we will skip the first record,
     * FALSE means we will not skip the first record
     */
    $SKIP_FIRST_RECORD TRUE;

    if(isset(
    $_POST['submit'])) {
      
    /* make sure the file exists before trying to open it*/
      
    if(!file_exists($_POST['filename']))
        die(
    "That file does not exist!");

      
    /* open it for reading */
      
    $file = new SplFileObject($_POST['filename'], 'r');

      
    /* if we're already at the end of the file, it must be empty */
      
    if($file->eof())
        die(
    "Empty File! No records were imported.");

      
    /* skip the first record (csv headings) */
      
    if($SKIP_FIRST_RECORD)
        
    $file->fgetcsv();

      
    /* a counter to keep track of the number of records inserted */
      
    $numberOfRowsInserted 0;
      
    /* the MySQL query template */
      
    $query "INSERT INTO Persons(`Firstname`,`Lastname`,`Age`,`Lots`) values('%s', '%s', '%s', '%s');";
      
    mysql_query("START TRANSACTION;");
      
    mysql_query("SET autocommit=0;");
      while(!
    $file->eof() && $data $file->fgetcsv() !== FALSE)  {
        if(!
    mysql_query(
            
    sprintf($import,
                
    mysql_real_escape_string($data[0]),
                
    mysql_real_escape_string($data[1]),
                
    mysql_real_escape_string($data[2]),
                
    mysql_real_escape_string($data[3])))) {
          
    /* if the query failed, we're aborting
           * you should close the file handle so that other processes
          * can use that resource if they need
          * it's good practice   */
          
    fclose($handle);
          
    /* exit after displaying the mysql error message */
          
    mysql_query("ROLLBACK;");
          
    printf("<pre>No records were inserted because an error was raised: ".mysql_error()."</pre>");
          
    mysql_close();
          exit;
        }
        
    $numberOfRowsInserted++;
      }
    }

    fclose($handle);
    mysql_query("COMMIT;");
    printf("Import done. %d rows inserted",$numberOfRowsInserted);

    }

  3. #3
    Join Date
    Nov 2012
    Posts
    2
    thanks in advance
    i will try this

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles