0
if ($_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 
    do { 
        if ($data[0]) { 
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 
    // 

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

} 

?> 

上記のインポート プロセスでは、csv ファイルの最初の行もデータベースに挿入されます。挿入中に最初の行を制限する方法を教えてください。

4

2 に答える 2

1
$lineNr = 1;
if ($_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 
    do { 
        if ($data[0] && $lineNr!=1) { 
            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email) VALUES 
                ( 
                    '".addslashes($data[0])."', 
                    '".addslashes($data[1])."', 
                    '".addslashes($data[2])."' 
                ) 
            "); 
            $lineNr++;
        } 
    } while ($data = fgetcsv($handle,1000,",","'")); 
    // 

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

} 
于 2013-04-15T09:39:35.007 に答える
0

使用する

fgetcsv($handle);
if ( $data = fgetcsv($handle,1000,",","'") )

do{ ...

そして、次のコマンドでファイルを閉じることを忘れないでください:

fclose( $handle );

リダイレクト前。

于 2013-04-15T09:40:01.783 に答える