我真的需要转义最后一个逗号来防止INSERT的SQL错误
我应该使用简单的内爆,也许是修剪?但是怎么样?我不想回显这些值,只是将它们插入到我的数据库中,转义最后一个逗号
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);
}
}
有人可以帮帮我吗? = D
编辑1:
TO CLARIFY:我需要阻止逗号进入$_POST['unidadeVestibular']
以保持循环继续并按原样插入