Solved

CSV Data Import without Header Row

Posted on 2016-08-27
1
42 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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

18 Experts available now in Live!

Get 1:1 Help Now