<?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     = "product.csv";
$destfile = $destdir.$name;  

$batch_size = 3000;
$table_name = "product";
$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("Internalproductcode","Externalproductcode","KitCode","Type","ItemType","ItemTaxCode","Category","Item","Description","LoadCapacityMin","LoadCapacityMax","CapacityText","Lenght","LengthText","Width","WidthText","Height","HeightText","LoadCells ","Cables","SignalCables","Weight","Remarks","IsActive","Price","CostingMethod");
			foreach($variable_array as $key=>$val){		
				${$val} = addslashes(trim($buffer[$key]));
			}
			if($Internalproductcode =="Internalproductcode"){
				continue;
			}
			
			$coun_qry = "SELECT id FROM $table_name WHERE internal_product = '$Internalproductcode'";
			$coun_sql = mysqli_query($conn,$coun_qry);
			$coun_id = "";
			if(mysqli_num_rows($coun_sql)>0){
				$data1 = mysqli_fetch_array($coun_sql);
				$coun_id = $data1['id'];
				
				$sql_update = "country = '".$country."',zone = '".$zone."'";
				$sql_update = "UPDATE $table_name SET ".$sql_update." WHERE id = '".$coun_id."'";
				if(mysqli_query($conn,$sql_update)){
					$records_updated++;
				}					
			}else{				
				$sql = "internal_product = '".$Internalproductcode."',external_product = '".$Externalproductcode."',kit_code = '".$KitCode."',type = '".$Type."',product_type = '".$ItemType."',itemtax_code = '".$ItemTaxCode."',category = '".$Category."',product = '".$Item."',description = '".$Description."',load_capacity_min = '".$LoadCapacityMin."',load_capacity_max = '".$LoadCapacityMax."',capacity_text = '".$CapacityText."',lenght = '".$Lenght."',lenght_text = '".$LengthText."',item_width = '".$Width."',width_text = '".$WidthText."',height = '".$Height."',height_text = '".$HeightText."',load_cells = '".$LoadCells."',cables = '".$Cables."',signal_cables = '".$SignalCables."',weight = '".$Weight."',remarks = '".$Remarks."',is_active = '".$IsActive."',price = '".$Price."',costing_method = '".$CostingMethod."' ";
				$sql= "INSERT INTO $table_name SET ".$sql;
				
				if(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>';
}?>
 <?php 
if($records_updated){    
  echo '<br><span style="font-family:calibri;color:red;">'.($records_updated).' records has been updated...</span>';
}?>