我正在使用PHP文件从CSV文件读取值并将其导入到mysql数据库中。工作正常,但现在我在一个字段中带有撇号的CSV文件出现错误:
CSV文件:
...
21.08.2018;21.08.2018;Peter Muller;Voucher;6 LED's for Kitchen Lamp;19,40;EUR;1000
...
问题:文本中有撇号。
我的PHP插入语句:
<?php
...
$sqlinsert = "INSERT INTO `" . $tablename . "` "
."(`BankID`, `Date`, `Valuta`,
`Topic`, `Type`,
`Text`, `Amount`,
`Currency`, `Balance`) "
."VALUES "
. "("
."'$id', " // BankID
."'$csvdata[0]' , " // Date
."'$csvdata[1]' , " // Valua
."'$csvdata[2]' , " // Topic
."'$csvdata[3]' , " // Type
."'$csvdata[4]' , " // Text
."'$csvdata[5]' , " // Amount
."'$csvdata[6]' , " // Currency
."'$csvdata[7]'" // Balance
.") "
."ON DUPLICATE KEY UPDATE "
."BankID = VALUES(BankID), "
."Text = VALUES(Text); ";
# echo "<b>SQL: </b> . $sqlinsert . <br>";
$insCmd = $pdo->prepare($sqlinsert);
$insCmd->execute();
错误消息是:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
由于我是PDO的新手,我该如何操纵$csvdata[4]
来处理完美的插入? -还是有特殊的PDO命令来管理?