SQL在FOR循环中执行两次

I think I need a second pair of eyes on this one. For the life of me I can't figure out why my SQL INSERT query is running twice every iteration:

if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($current_row == 1 || $current_row == 2) {
        $num = count($data);
        //echo "<p> $num fields in line $row: <br /></p>
";
        $row++;
        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />
";
            try {
                set_time_limit(0);
                $stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
                $stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1])); }
            catch(PDOException $e) {
                echo 'ERROR: ' . $e->getMessage();
                exit; }
            }
        }
    $current_row++;
    }
fclose($handle);
}

Well, I probably should have put forth a little more effort before asking for assistance. I was able to fix this by adding a row reset counter in the loop:

if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($current_row == 1 || $current_row == 2) {
        $row_reset = 0;
        $num = count($data);
        //echo "<p> $num fields in line $row: <br /></p>
";
        $row++;
        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />
";
            if ($row_reset == 0) {
                try {
                    set_time_limit(0);
                    $stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
                    $stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1])); }
                catch(PDOException $e) {
                    echo 'ERROR: ' . $e->getMessage();
                    exit; }
                }
            $row_reset++;
            }
        }
    $current_row++;
    }
fclose($handle);
}

I'm still curious if there is a better way.

I looks like you were using some code to iterate through your columns, but actually only have two, containing the sku and prod ($data[0] and $data[1].) If that is the case, then you don't need the for loop inside. (BTW, it was that loop that was causing the query to be executed twice, as the query was inside it.) It also looks like you have two counters going for the row (current_row and row) so those can be combined. If you are using the if ($currentRow == 1... statement to ignore the header on row 0 and then only process 2 rows, you will want to switch it to if ($currentRow > 0) when you are ready to run the whole spreadsheet. Using break in the catch clause instead of exit will cause the code to drop out of the while loop and hit the fclose statement:

if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
$currentRow = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($currentRow == 1 || $currentRow == 2) {
        $num = count($data);
        //echo "<p> $num fields in line $currentRow: <br /></p>
";
        //echo "<p> The data for this row is: " . $data[0]  . " " . $data[1] . "
";
        try {
            set_time_limit(0);
            $stmt = $db_temp_kalio->prepare('INSERT INTO invupdate (sku,prod) VALUES(:sku, :prod)');
            $stmt->execute(array(':sku'=> $data[0], ':prod'=> $data[1]));
        }
        catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
            break;
        }
    }
    $currentRow++;
}
fclose($handle);
}