Uncaught exception 'PDOException' with message 'SQLSTATE[42000

10,395

Solution 1

You need to escape reserved words like ADD with backticks:

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

Solution 2

ADD is a reserved keyword and happens to be the name of your column. To avoid syntax error, you need to escape it using backtick. eg,

`ADD`

If you have the privilege to alter the table, change the column name to which is not a reserved keyword to avoid problem from occuring again.

Share:
10,395

Related videos on Youtube

Jonathan Thurft
Author by

Jonathan Thurft

Updated on September 15, 2022

Comments

  • Jonathan Thurft
    Jonathan Thurft over 1 year

    I am trying to update a table from a CSV. The following error appears when trying to insert a row. Other queries go through in the same loop but it stops there on a particular one.

    I've checked if all the values match exactly with the DB table and they do.

    I am out of ideas on what could be the issue. Any suggestions?

    The Query

    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)
    

    The error

    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
    

    The Code

      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();
                    }
    
  • berserk
    berserk over 9 years
    in my case, it was 'condition'.