I have a code that will allow users to import/insert data into database from the CSV file. the problem is that if there is an empty columns or cells from the CSV file, it will display an error telling me that the column count doesn't match value count at row 1. I tried to assign a value in the empty cells/columns of the CSV file and it successfully uploaded. How can I allow the users to upload a CSV file even if there's an empty columns/cells? Below is my code.
if ($handle) {
$counter = 0;
$sql ="INSERT IGNORE INTO bill_of_materials(allotment_code, category_name, activity, quantity, unit, description, end_unit_quantity, unit_cost, regular_labor_cost, end_unit_labor_cost, type, batch) VALUES";
while (($line = fgets($handle)) !== false) {
$sql .= "('".implode("', '", explode("|", addslashes(str_replace('"', '', $line))))."'),";
$counter++;
}
$sql = substr($sql, 0, strlen($sql) - 1);
if (mysqli_query($new_conn, $sql) === TRUE) {
$message = 1;
} else {
echo mysqli_error($new_conn);
}
fclose($handle);