我真的需要转义最后一个逗号,以防止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:
需要说明的是:我需要防止在$_POST['unidadeVestibular']
中插入逗号,以保持循环的进行,并按照应该做的方式插入
使用rtrim()函数从字符串的末尾剥离任何字符
rtrim($string, ",")
可能的解决方案
$statement = $pdo->prepare(rtrim($string, ","));
$statement->execute();
rtrim删除字符串最后一个字符的替代方法是使用substr。以下内容将提取部分$string
(除最后一个字符外的所有内容)并重新分配给$string
。
$string = substr($string, 0, -1);