MySQL Error - "You Have a SQL Syntax Error"

The error message I received is:

You have an error in the SQL syntax; check the manual corresponding to the version of your MySQL server for the correct syntax for use next to "word", "group", "self-observation"). VALUES ("item", "a", "self note") "on line 1

PHP code:

$toq="INSERT INTO articles ('word','group','selfnote') VALUES ('$ttle','$wrdr','$snote')"; 

I tried to find solutins, but they didn't seem to work as the echo gives:

 INSERT INTO articles ('word','group','selfnote') VALUES ('item','a','note to self') 

which seems pleasant to me. What is the problem?

+4
source share
4 answers

Use backticks instead of quotes to remove names. Quotes are line breaks.

 $toq="INSERT INTO articles (`word`,`group`, `selfnote`) VALUES ('$ttle','$wrdr','$snote')"; 
+7
source

You put quotation marks in field names. This forces MySQL to treat them as strings, not field names, and you cannot insert them into strings.

 INSERT INTO articles (word, group, selfnote) VALUES (....); 

- The correct syntax. The only citation type allowed for field names is to use reverse steps to exit reserved text fields, for example.

 INSERT INTO articles (table, int, varchar) ... 

will fail due to the use of 3 reserved words, but adding reverse steps

 INSERT INTO articles (`table`, `int`, `varchar`) ... 

makes them acceptable as field names.

+8
source

You should not specify column names with normal quotation marks ( '' ), rather use reverse backticks ( `` ).

+4
source

You must remove or replace the column quotation marks with reverse loops (`). Since "group" is a keyword, you should use backlinks:

 INSERT INTO articles (`word`, `group`, `selfnote`) VALUES (....); 
+2
source

Source: https://habr.com/ru/post/1393637/


All Articles