PHP 中的语法错误或访问冲突

Syntax error or access violation in PHP

尝试运行以下代码时,

$query = "INSERT INTO check (name, number) values ";
$query .= implode(',', $read_data); 
$stmt = $this->pdo->prepare($query);
$stmt->execute();

其中 $read_data 是一个数组,其值为 ('aaa','000'),('bbb','001'),('ccc','002')

我收到一个错误,因为 语法错误访问冲突

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'check (name, number) values ('aaa','000'),('bbb','001' at line 1

$query = "INSERT INTO check ..." 更改为 $query = "INSERT INTO `check` ..."CHECK 是 SQL 中的保留字。

您的查询语法不正确:

这种类型的数组可以,但是字符串末尾需要有单引号和分号。

$read_data = array("('aaa','000')","('bbb','001')","('ccc','002')");
$query = "INSERT INTO `check` (name, number) values ";
$query .= implode(',', $read_data).";";
echo $query;

给我:

INSERT INTO `check` (name, number) values ('aaa','000'),('bbb','001'),('ccc','002');