My CSV file has got some additional commas within a particular column which is causing the problem. When I import it into MySQL, the data in that particular column is split into 2 columns. Could you please help me to fix this? Thanks.
NOTE 1: I'm not allowed to use Load Data Local Infile on my server so this is not an option to be considered.
NOTE 2: I'm not well versed in PHP so I would appreciate it if you would show the the correct code.
NOTE 3. The additional commas are in the $Club_Name column. However there is a possibility that additional commas could also appear in other columns in the future so I'm looking for a complete solution to clean up all irrelevant commas in all columns.
My code (after days of struggle):
<?php
include("/path/to/the/database/connection/db_connect.php");
$filename = fopen("/path/to/the/csv/file/clubperformance_District_102.csv", "r");
//open the csv file
if ($filename) {
$firstline = fgets($filename, 4096);
while (($line = fgets($filename)) !== false) {
$lineArr = explode(",", "$line");
var_dump($lineArr); // to display the array
$lineArr = preg_replace("/[^a-zA-Z0-9 ]/", "", $lineArr); //clean up everything except alpha numeric characters & spaces
// list all the variables representing the database columns
list($District, $Division, $Area, $Club_Number, $Club_Name, $Club_Status, $Mem_Base, $Active_Members, $Goals_Met, $CCs, $Add_CCs, $ACs, $Add_ACs, $CL_AL_DTMs, $Add_CL_AL_DTMs, $Level_1, $Level_2, $Add_Level_2, $Level_3, $Level_4, $Level_5, $New_Members, $Add_New_Members, $Off_Trained_Round_1, $Off_Trained_Round_2, $Mem_dues_on_time_Oct, $Mem_dues_on_time_Apr, $Off_List_On_Time, $Club_Distinguished_Status, $Last_Updated) = $lineArr;
// and then insert data
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO `toastmaster_dcp_tracking_old`(`District`, `Division`, `Area`, `Club_Number`, `Club_Name`, `Club_Status`, `Mem_Base`, `Active_Members`, `Goals_Met`, `CCs`, `Add_CCs`, `ACs`, `Add_ACs`, `CL_AL_DTMs`, `Add_CL_AL_DTMs`, `Level_1`, `Level_2`, `Add_Level 2`, `Level_3`, `Level_4`, `Level_5`, `New_Members`, `Add_New_Members`, `Off_Trained_Round_1`, `Off_Trained_Round_2`, `Mem_dues_on_time_Oct`, `Mem_dues_on_time_Apr`, `Off_List_On_Time`, `Club_Distinguished_Status`, `Last_Updated`) VALUES ('$District', '$Division', '$Area', '$Club_Number', '$Club_Name', '$Club_Status', '$Mem_Base', '$Active_Members', '$Goals_Met', '$CCs', '$Add_CCs', '$ACs', '$Add_ACs', '$CL_AL_DTMs', '$Add_CL_AL_DTMs', '$Level_1', '$Level_2', '$Add_Level_2', '$Level_3', '$Level_4', '$Level_5', '$New_Members', '$Add_New_Members', '$Off_Trained_Round_1', '$Off_Trained_Round_2', '$Mem_dues_on_time_Oct', '$Mem_dues_on_time_Apr', '$Off_List_On_Time', '$Club_Distinguished_Status', '$Last_Updated')")or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
}
fclose($filename);
}
?>