Solved

CSV Data Import without Header Row

Posted on 2016-08-27
1
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 57

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

688 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