Anon Anon - 30 days ago 6
PHP Question

SQLSTATE [42000] when adding data

When parsing an xml file and adding it to the database, it displays an error during the script operation:

$sql->exec("INSERT INTO 'example' VALUES('$id', '$title', '$link')");



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 ''example'
VALUES('phpmysqljquery-przeciagnij-z-wy'
at line 1


Can anyone say what is wrong?

EDIT:
My code:

$sql->exec("INSERT INTO `example` VALUES (:id,:title,:link)");
$sql->bindParam(':id', $id, PDO::PARAM_STR);
$sql->bindParam(':title', $title, PDO::PARAM_STR);
$sql->bindParam(':link', $link, PDO::PARAM_STR);
$sq->execute();


Error:


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
':id,:title,:link)' at line 1

Answer Source

You're using the wrong quoting style. Database, table and column identifiers use a different approach:

INSERT INTO `example` VALUES (?,?,?)

Double and single quotes are only for strings. You can't insert stuff into a string.