-1
    "code": "ER_PARSE_ERROR",
    "errno": 1064,
    "sqlMessage": "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 'INSERT INTO users(email, first_name, last_name, password, role_id) values(\"swsws' at line 1",
    "sqlState": "42000",
    "index": 0,

I get the this error in NodeJs the I try do to this query with multiple inserts. The query works is phpmyadmin so I don't get where the problem is. Please help me if you can, I am trying to fix it for hours.

             let q1= `BEGIN`,
             q3= `SET @last_id_in_users = LAST_INSERT_ID()`,
             q4= `INSERT INTO sale_targets (user_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday) VALUES(LAST_INSERT_ID(), '0', '0','0','0','0','0','0')`,
             q5= `INSERT INTO close_ratio (user_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday) VALUES(@last_id_in_users,'0', '0','0','0','0','0','0')`,
             q6= `INSERT INTO lead_targets (user_id, monday, tuesday, wednesday, thursday, friday, saturday, sunday) VALUES(@last_id_in_users,'0', '0','0','0','0','0','0')`,
             q7= `COMMIT`
             let valuesStr = `("${rBody.email}", "${rBody.firstName}", "${rBody.lastName}", "${hash}", ${roleId})`,
             q2 = `INSERT INTO users(email, first_name, last_name, password, role_id) values${valuesStr}`,
             q = `${q1}; ${q2}; ${q3}; ${q4}; ${q5}; ${q6}; ${q7};`;
  • 1
    Can you log vars rBody.xxx and valueStr and q2, and q, and post it ? Maybe a escape string issue ? try to set rBody.email with "XXX" and idem for others var firstname , lastName, hash, and roleId with 1. – Eric Mar 19 '20 at 00:28

1 Answers1

0

Do queries one at a time. You can't concatenate them with ';'

danblack
  • 12,130
  • 2
  • 22
  • 41