-1

I'm not sure if this is a bug, but I cannot insert a row into my MySQL table using a query in Node.JS.

Degree Table: Degree Table

Node.JS code utilizing MySQL2:

await mySqlHelper
  .queryDatabaseAsync(
    "INSERT INTO Degree VALUES( id = '" +
      uuid +
      "', userId = '" +
      data.userId +
      "', degreeType = '" +
      data.degreeType +
      "', major = '" +
      data.major +
      "', yearInProgram = '" +
      data.yearInProgram +          
      "', graduationDate = '" +
      new Date(data.graduationDate).getFullYear() +
      "-" +
      (new Date(data.graduationDate).getMonth() + 1) +
      "-" +
      new Date(data.graduationDate).getDate() +
      "');"
  )

And the response I get is this:

Error: Incorrect date value: '0' for column 'graduationDate' at row 1

sql: "INSERT INTO Degree VALUES( id = '12345678-1234-1234-1234-123456789123', userId = '12345678-1234-1234-1234-123456789123', degreeType = 'PhD', major = 'Engineering', yearInProgram = '3', graduationDate = '2030-9-1');", 

sqlMessage: "Incorrect date value: '0' for column 'graduationDate' at row 1"

I've tried formatting it every way I can, and I do not know why it says it has the value '0'.

Thanks!

  • 1
    please add the create table also. the insert is syntactical correct and the date is accepted in mysql 8, but still to prevent **sql injection** use **prepared statements with parameters** see https://stackoverflow.com/questions/15778572/preventing-sql-injection-in-node-js – nbk Sep 01 '23 at 20:13
  • @nbk okay, I just added the MySQL table screenshot. It's a DATE column, not a DATETIME column, so I thought I could store it like that. These are two of the already entered dates: "2024-12-01", "2025-09-05" – PythonIsCool Sep 01 '23 at 20:26
  • images are not allowed see https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question and plese read the link with the sq injection – nbk Sep 01 '23 at 20:29
  • @PythonIsCool you have a mistake with insert syntax! Please look for my edited answer! – adampweb Sep 01 '23 at 20:30

1 Answers1

0

Your insert statement is not valid!

The correct syntax is: INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3);

adampweb
  • 1,135
  • 1
  • 9
  • 19