未捕获的异常'PDOException',消息'SQLSTATE [42000

时间:2013-11-07 10:45:39

标签: php mysql pdo

我正在尝试从CSV更新表格。尝试插入行时出现以下错误。其他查询在同一个循环中进行,但它会在特定循环中停止。

我已经检查过所有值是否与数据库表完全匹配,而且确实存在。

我对可能出现的问题缺乏想法。有什么建议吗?

查询

INSERT INTO user_interests (member_num,ABU,ADD,ADOP,ANO,ANX,BER,BULL,COMP,CONF,CONT,CUL,DEP,DIS,DOM) VALUES (:member_num,:ABU,:ADD,:ADOP,:ANO,:ANX,:BER,:BULL,:COMP,:CONF,:CONT,:CUL,:DEP,:DIS,:DOM)

错误

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: 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 'ADD,ADOP,ANO,ANX,BER,BULL,COMP,CONF,CONT,CUL,DEP,DIS,DOM) VALUES ('5323',1,1,1,1' at line 1' in /Applications/XAMPP/xamppfiles/htdocs/ukcp/phpLibraries/users/userPopulateInts.php:45 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/ukcp/phpLibraries/users/userPopulateInts.php(45): PDOStatement->execute() #1 /Applications/XAMPP/xamppfiles/htdocs/ukcp/userprofile.php(14): userPopulateInts->ints1() #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/ukcp/phpLibraries/users/userPopulateInts.php on line 45

守则

  if ( !empty($int) && !empty($intColon) ) {
                    $sql = 'INSERT INTO user_interests (member_num,'. implode(",", $int) .') VALUES (:member_num,'. implode(",", $intColon) .')';
                    $dbQuery = $this->db()->prepare($sql);
                    echo $sql ."<br>";
                    $dbQuery->bindValue(":member_num", $data[0], PDO::PARAM_INT);
                    foreach ($intColon as $val) {
                        $dbQuery->bindValue($val, 1, PDO::PARAM_INT);
                    }

                    $dbQuery->execute();
                }

3 个答案:

答案 0 :(得分:4)

ADD是保留关键字,恰好是列的名称。要避免语法错误,您需要使用反引号来转义它。例如,

`ADD`

如果您有权更改表,请将列名更改为不是保留关键字,以避免再次出现问题。

答案 1 :(得分:3)

你需要像ADD一样使用反引号逃避reserved words

INSERT INTO user_interests (member_num, ABU, `ADD`, ...

答案 2 :(得分:0)

ADD是MySQL保留的关键字,如here所述。请转义表名,或选择不冲突的更好名称。