Maciej M Maciej M - 1 year ago 45
MySQL Question

I try to make INSERT WHERE in SQL, but it gives me an error

When I try to insert data where with code:

$query = dbConnect()->prepare("INSERT INTO users(key) WHERE mail='$mail' VALUES ('$key')");

I'm using XAMPP, it gives me an error:

Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'key) WHERE mail='maciej@localhost' VALUES (key)' at line 1 in C:\xampp\htdocs\PHP7_login\restore\index.php:38

Answer Source

You should use backticks for key (because is a reserved word)
and not use where

"INSERT INTO users(`key`)  VALUES ('$key')"

or if you need an update

"UPDATE users
 set  `key` = '$key'
 where mail = '$mail'"