<?php
set_time_limit(0);
ini_set('memory_limit', '1024M');

include "config.php"; 

$destfile = "live-data-migration-csvs/VAC_Set_instruction.csv";
$table_name = "set_route_details";

$records_updated = 0;
$records_skipped = 0;
$records_not_found = 0;

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

if (!$fp) {
    die("Error: Unable to open CSV file.");
}

// Skip header
fgetcsv($fp, 1000000, ',');

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

    $buffer = array_map('trim', $buffer);

    if (count(array_filter($buffer)) === 0) {
        continue;
    }

    // Only need 2 columns
    if (count($buffer) < 2) {
        $records_skipped++;
        continue;
    }

    list($Set_No, $Plate_Size) = $buffer;

    $Set_No = mysqli_real_escape_string($conn, $Set_No);
    $Plate_Size = mysqli_real_escape_string($conn, $Plate_Size);

$query = "SELECT id FROM set_mangmnt WHERE set_number = '$Set_No'";
echo $query;
    $check = mysqli_query($conn, $query);
echo "<br>";
    if (mysqli_num_rows($check) > 0) {

        $row = mysqli_fetch_assoc($check);
        $id = $row['id'];

 echo       $sql = "UPDATE $table_name 
                SET setting_instruction = '$Plate_Size'
                WHERE set_id = '$id'";
//die;
        if (mysqli_query($conn, $sql)) {
            $records_updated++;
        } else {
            $records_skipped++;
        }

    } else {
        $records_not_found++;
    }
}

fclose($fp);

// OUTPUT
echo "<br><span style='color:green;'>$records_updated records updated.</span>";
echo "<br><span style='color:orange;'>$records_skipped records skipped.</span>";
echo "<br><span style='color:red;'>$records_not_found records not found.</span>";
?>