Try this script I made for you, it looks a bit more robust to me.
If you want to skip the first record, just use fgetcsv() on the csv and don't store the row of values anywhere (just toss it out).
<?php
/* include database credentials and open mysql connection */
include "connect.php";
/**
* a flag that decides if we should skip the first record in the CSV (column headings) or not
* TRUE means we will skip the first record,
* FALSE means we will not skip the first record
*/
$SKIP_FIRST_RECORD = TRUE;
if(isset($_POST['submit'])) {
/* make sure the file exists before trying to open it*/
if(!file_exists($_POST['filename']))
die("That file does not exist!");
/* open it for reading */
$file = new SplFileObject($_POST['filename'], 'r');
/* if we're already at the end of the file, it must be empty */
if($file->eof())
die("Empty File! No records were imported.");
/* skip the first record (csv headings) */
if($SKIP_FIRST_RECORD)
$file->fgetcsv();
/* a counter to keep track of the number of records inserted */
$numberOfRowsInserted = 0;
/* the MySQL query template */
$query = "INSERT INTO Persons(`Firstname`,`Lastname`,`Age`,`Lots`) values('%s', '%s', '%s', '%s');";
mysql_query("START TRANSACTION;");
mysql_query("SET autocommit=0;");
while(!$file->eof() && $data = $file->fgetcsv() !== FALSE) {
if(!mysql_query(
sprintf($import,
mysql_real_escape_string($data[0]),
mysql_real_escape_string($data[1]),
mysql_real_escape_string($data[2]),
mysql_real_escape_string($data[3])))) {
/* if the query failed, we're aborting
* you should close the file handle so that other processes
* can use that resource if they need
* it's good practice */
fclose($handle);
/* exit after displaying the mysql error message */
mysql_query("ROLLBACK;");
printf("<pre>No records were inserted because an error was raised: ".mysql_error()."</pre>");
mysql_close();
exit;
}
$numberOfRowsInserted++;
}
}
fclose($handle);
mysql_query("COMMIT;");
printf("Import done. %d rows inserted",$numberOfRowsInserted);
}