I really need to escape the last comma to prevent SQL errors with the INSERT
Should I use a simple implode, maybe a trim? But how? I don't want to echo those values, just insert them into my database escaping the last comma
if ($handle = fopen($arquivo['tmp_name'], "r")) {
$pdo = $this->connector->getConnection();
$firstLine = true;
$string = "INSERT INTO vestibular (rg, nome, curso, resultado, colocacaogeral, colocacaocurso, unidade) VALUES ";
while ($row = fgetcsv($handle , 0 , ";")) {
if ($firstLine) {
$firstLine = false;
continue;
}
$string .= "(
'" . mb_convert_encoding($row[0], 'UTF-8', 'ISO-8859-1') . "',
'" . mb_convert_encoding($row[1], 'UTF-8', 'ISO-8859-1') . "',
'" . mb_convert_encoding($row[2], 'UTF-8', 'ISO-8859-1') . "',
'" . mb_convert_encoding($row[3], 'UTF-8', 'ISO-8859-1') . "',
'" . mb_convert_encoding($row[4], 'UTF-8', 'ISO-8859-1') . "',
'" . mb_convert_encoding($row[5], 'UTF-8', 'ISO-8859-1') . "',
'" . $_POST['unidadeVestibular'] . "'
),";
}
$statement = $pdo->prepare($string);
$statement->execute();
return $statement;
fclose($handle);
}
}
Could someone help me please? =D
EDIT 1 :
TO CLARIFY: I need to prevent a comma into $_POST['unidadeVestibular']
to keep the loop going and Insert as it should do