Jonathan Thurft Jonathan Thurft - 3 months ago 11
MySQL Question

Uncaught exception 'PDOException' with message 'SQLSTATE[42000

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();
}

Answer

You need to escape reserved words like ADD with backticks:

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