<?php
//die("hii");
set_time_limit(0);
ini_set('memory_limit','1024M');

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

$records_updated = 0;
$records_not_found = 0;  

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

$line = 0;

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

    $line++;

   
    if ($line == 1) continue;

   
    $PartNumber        = addslashes(trim($buffer[0]));
    $ProductDescription = addslashes(trim($buffer[1]));
    $StandardCost       = addslashes(trim($buffer[2]));


    $checkSql = "
        SELECT id FROM item_management 
        WHERE part_number = '$PartNumber'
        LIMIT 1
    ";
    $checkResult = mysqli_query($conn, $checkSql);

    if (mysqli_num_rows($checkResult) > 0) {
       
        $sql_update = "
            UPDATE item_management
            SET product_description = '$ProductDescription',
                standard_cost_ebq = '$StandardCost'
            WHERE part_number = '$PartNumber'
        ";
        mysqli_query($conn, $sql_update);

        $records_updated++;

    } else {
   
        $records_not_found++;
    }
}

fclose($fp);
?>

<?php 
if ($records_updated) {    
  echo '<br><span style="font-family:calibri;color:green;">Updated Records: '.$records_updated.'</span>';
}

if ($records_not_found) {    
  echo '<br><span style="font-family:calibri;color:red;">Part Not Found: '.$records_not_found.'</span>';
}
?>
