Solved

CSV Data Import without Header Row

Posted on 2016-08-27
1
44 Views
Last Modified: 2016-08-27
Hi,

I'm using PHP to import a csv file using the following code ... the mapping to all the fields isn't yet complete I'm just testing the logic. However, I need to omit the first row as it's pulling in the header data for the file which isn't need.

How do I do that?

J

<!DOCTYPE html>
<?php include("connection_1.php"); 


    if(isset($_POST['submit']))
        
    {
        
        $file = $_FILES['file']['tmp_name'];
        
        $handle = fopen($file,"r");
        
        while(($fileop = fgetcsv($handle,1000,",")) !== false)
        
            {
            
            $sales_record_number = $fileop[0];
            $user_id = $fileop[1];
            $buyer_email = $fileop[4];
            $buyer_name = $fileop[2];
            $buyer_phone = $fileop[3];
            $add_1 = $fileop[5];
                
            $query = "INSERT into holding_ebay 
                                        
                                        (    sales_record_number
                                            ,user_id
                                            ,buyer_email
                                            ,buyer_name
                                            ,buyer_phone
                                            ,add_1) 
                                            
                                            VALUES
                                            
                                        (   '$sales_record_number'
                                            ,'$user_id'
                                            ,'$buyer_email'
                                            ,'$buyer_name'
                                            ,'$buyer_phone'
                                            ,'$add_1')";
            mysqli_query($link, $query);
            
            }
                
            if (mysqli_query($link, $query))    {
                    echo "The data import was successful";
                                                }
else {
  echo "The data import failed with the following error: " . mysqli_error($link);
}
    }
    
?>

Open in new window

0
Comment
Question by:Ridgejp
1 Comment
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41772846
Just add an fgets() before the while loop as shown below
  ...
  $handle = fopen($file,"r");
  // Add a fgets here
  fgets($handle);
  while(($fileop = fgetcsv($handle,1000,",")) !== false)
  ...

Open in new window

Refer to Solution #2 in this thread (https://www.experts-exchange.com/questions/28965947/CSV-into-MySQL-Using-PHP.html#a41772482) for further examples.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now