<?php 
die("hii");
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     = "customer_details.csv";
$destfile = $destdir.$name;

$batch_size = 3000;
$table_name = "client_management";
$records_inserted = 0;
$records_updated = 0;

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

while (!feof ($fp)){

    $buffer = fgetcsv($fp, 1000000, ',');

    if($flag) {

        if($buffer){

            $variable_array = array("Customer_Account_Code","Customer_Name");

            foreach($variable_array as $key=>$val){
                ${$val} = addslashes(trim($buffer[$key]));
            }

            if($Customer_Account_Code =="Customer_Account_Code"){
                continue;
            }

            $created_by = $_SESSION['user_id'];
            $created_on = date("Y-m-d H:i:s");
            $updated_by = $_SESSION['user_id'];

            // CHECK IF CUSTOMER EXISTS
            $check_sql = "SELECT customer_code FROM $table_name 
                          WHERE customer_code = '$Customer_Account_Code' 
                          LIMIT 1";
            $check_res = mysqli_query($conn,$check_sql);

            if(mysqli_num_rows($check_res) > 0){

                // UPDATE
                $sql = "UPDATE $table_name 
                        SET customer_name = '$Customer_Name'
                        WHERE customer_code = '$Customer_Account_Code'";
                mysqli_query($conn,$sql);

                $records_updated++;

            }else{

                // INSERT
                $sql = "INSERT INTO $table_name(`customer_name`,`customer_code`) 
                        VALUES('$Customer_Name','$Customer_Account_Code')";
                mysqli_query($conn,$sql);

                $records_inserted++;
            }
        }
    }
}
?>
<?php
if($records_inserted){
    echo '<br><span style="font-family:calibri;color:red;">'.$records_inserted.' records has been inserted...</span>';
}

if($records_updated){
    echo '<br><span style="font-family:calibri;color:red;">'.$records_updated.' records has been updated...</span>';
}
?>