I am trying to make a PDO MYSQL statement to insert only data that NOT exists in the database
A simple example with PHP
$sql = INSERT INTO mytable SET hash=:hash, tablename=:tablename, recuid=:recuid WHERE recuid NOT IN (SELECT recuid FROM mytable);
$insert = $this->_pdo->prepare($sql);
$insert->execute(array(
':hash' => $this->_getHash,
':tablename' => $this->_catNewTable,
':recuid' => $result->uid
));
this->_pdo
connects to the database which works finethis->_getHash
generates a random hash Code- recuid is numeric
- tablename is a string
var_dump result:
string(185) "INSERT INTO sys_refindex SET hash=:hash, tablename=:tablename, recuid=:recuid WHERE recuid NOT IN (SELECT recuid FROM sys_refindex)"
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 'WHERE recuid NOT IN (SELECT recuid FROM sys_refindex)' at line 1SQLSTATE[42000]
If think there is a problem with the WHERE statement combined with the placeholders?