error while inserting multiple inputs in sql

39 Views Asked by At

This is the query I used const dbQuery = INSERT INTO tasks (activity,type,participants,price,link,key,accessibility) VALUES ('${activity}','${type}','${participants}','${price}','${link}','${key}','${accessibility}');

This is the error I receive: "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 'key,accessibility) VALUES ('Repaint a room in your house','recreational','1','0.' at line 1"

The value for key received is like "4877086"

I think it is a datatype problem in sql, so tried using bigint, varchar and int but still got the same error. Also tried to convert the key to a number. If i remove the key from the syntax I receive the result perfectly.(Using Tableplus for MySql)

1

There are 1 best solutions below

1
On BEST ANSWER
  • The word "key" is a reserved keyword in MySQL.

  • Avoid using it as a column name.

  • If you must use it, you can enclose it in backticks (`) to indicate that it is a column name and not a keyword.

     const dbQuery = `INSERT INTO tasks (activity, type, participants, price, link, \`key\`, accessibility) 
     VALUES ('${activity}', '${type}', '${participants}', '${price}', '${link}', '${key}', '${accessibility}')`;
    
  • Check if this is the Cause for your issue.