-1

I am trying to send CSV data into my mysql database but i want to skip first row of the data because it contains header, i went through some post in here but they were not helpfully any idea how can i perform this in my below codes?????

//if ($_FILES['csv']['size'] > 0) { 
if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0) { 
//get the csv file 
$file = $_FILES['csv']['tmp_name']; 
$handle = fopen($file,"r"); 

//loop through the csv file and insert into database
$num = count($data); 
do { 
    if ($data[0]) { 
        mysql_query("INSERT INTO mbet_football (TIME, TEAMS, HOME, DRAW, AWAY) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                '".addslashes($data[2])."',
                '".addslashes($data[3])."',
                '".addslashes($data[4])."' 
            ) 
        "); 
    } 
} while ($data = fgetcsv($handle,1000,",","'")); 
// 

//redirect 
header('Location: mbetf.php?success=1'); die; 

 } 

 ?>
Leo
  • 7,274
  • 5
  • 26
  • 48
Ruckser
  • 47
  • 1
  • 9

3 Answers3

3

Why not just get the first line before the loop?

$headers = fgetcsv($handle, 1000, ",", "'");

//loop through the csv file and insert into database
while($data = fgetcsv($handle, 1000, ",", "'")) {
    //code
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

I little bit changed your code, but it should work:

<?php

  //if ($_FILES['csv']['size'] > 0) { 
  if (!empty($_FILES['csv']['size']) && $_FILES['csv']['size'] > 0) { 
//get the csv file 
$file = $_FILES['csv']['tmp_name']; 

$row = 0;

if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {   

        $row++;

        if($row == 1) continue;

        if ($data[0]) { 
            mysql_query("INSERT INTO mbet_football (TIME, TEAMS, HOME, DRAW, AWAY) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."',
                    '".addslashes($data[3])."',
                    '".addslashes($data[4])."' 
                ) 
            "); 
        } 

    }
       fclose($handle);
   }

//redirect 
header('Location: mbetf.php?success=1'); die; 

 } 

 ?>
Just Vlad
  • 99
  • 1
  • 2
0

You can use a boolean flag to skip the first row.

//loop through the csv file and insert into database
$skipFlag = true;
while($data = fgetcsv($handle,1000,",","'")){
    if($skipFlag){
        $skipFlag = false;
        continue;
    }
    if ($data[0]) {
        mysql_query("INSERT INTO mbet_football (TIME, TEAMS, HOME, DRAW, AWAY) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                '".addslashes($data[2])."',
                '".addslashes($data[3])."',
                '".addslashes($data[4])."' 
            ) 
        "); 
    }
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37