<?php
die;
set_time_limit(0);
ini_set('memory_limit','1024M');
ini_set('post_max_size','32M');
ini_set('upload_max_filesize','32M');

$destdir  = "live-data-migration-csvs/";
$name     = "route_item_mapping.csv";
$destfile = $destdir.$name;  

$batch_size = 3000;
$table_name = "fake_item"; // change to your table
$records_inserted = 0;
$records_updated  = 0;  
$records_copied   = 0;


$fp = fopen($destfile, 'r');
$headerSkipped = false;

while (($buffer = fgetcsv($fp, 1000000, ',')) !== false) {

    if (!$buffer || count($buffer) < 2) {
        continue;
    }

    if (!$headerSkipped) {
        $headerSkipped = true;
        continue;
    }

    $variable_array = array("Route_Number", "Part_Number");
    foreach ($variable_array as $key => $val) {
        ${$val} = $conn->real_escape_string(trim($buffer[$key]));
    }

    // Insert query
    $sql = "INSERT INTO $table_name (Route_Number, Part_Number) 
            VALUES ('$Route_Number', '$Part_Number')";

    if ($conn->query($sql) === TRUE) {
        $records_inserted++;
    } else {
        echo "Error: " . $conn->error . "<br>";
    }
}
fclose($fp);
$conn->close();
?>

<?php 
if ($records_inserted) {    
    echo '<br><span style="font-family:calibri;color:red;">'.($records_inserted).' records inserted...</span>';
}
if ($records_updated) {    
    echo '<br><span style="font-family:calibri;color:green;">'.($records_updated).' records updated...</span>';
}
if ($records_copied) {    
    echo '<br><span style="font-family:calibri;color:blue;">'.($records_copied).' route rows duplicated with new item_id...</span>';
}
?>
