Insert Ignore into not working 在 php 中给出错误

Insert Ignore into not working is giving error in php

所以,我有一个 php 脚本,我在其中从数据源获取数据并将其保存到数组中,然后使用 php 将数组值插入到我的 psql 服务器中。

问题是我不想在我的数据库中有任何重复项,因为我会不断更新我的 table。所以我查找并发现 IGNORE INTO 以避免重复,但 php 抛出错误。

此外,由于我的 table 中没有任何主键,是否仍然可以实现这一点。

我的错误代码:

PHP Fatal error:  Uncaught Exception: Error attempting to query: PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near "IGNORE"

我的 php 用于在 psql 服务器中插入数据的脚本

$psql = $stm->pdo_prepared("Insert IGNORE INTO student(student_id,student_email,student_message,student_marks)VALUES".implode(',',$arry1());

提前致谢。

请 post 指出错误,但我相信您可能遇到了语法错误。您还需要大括号中的值和一些可能导致语法错误的空格。

$psql = $stm->pdo_prepared("INSERT IGNORE INTO student (student_id,student_email,student_message,student_marks) VALUES (".implode(',',$arry1()) . ")";

我还建议执行以下操作。您可以在此处查看更多信息:

http://php.net/manual/en/pdo.prepared-statements.php

$stmt = $dbh->prepare("INSERT IGNORE INTO student (student_id, student_email, student_message, student_marks) VALUES (:student_id, :student_email, :student_message, :student_marks)");
$stmt->bindParam(':student_id', $student_id);
$stmt->bindParam(':student_email', $student_email);
$stmt->bindParam(':student_message', $student_message);
$stmt->bindParam(':student_marks', $student_marks);