Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ER_PARSE_ERROR node.js and mysql issue (INSERT INTO)

I want to do a simple insert with Node.js while I am using socket.io with node.js and MySQL. Don't know why, but I am geting this error

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 ''markos'' at line 1

My code:
When I try this, I get the above error.

io.on("connection", function(socket){
console.log("a user is connected " + socket.id );


    socket.on("question", function (question){

        let sql = "INSERT INTO nodeJs (name) VALUES ?";

        con.query(sql, question, function (err) {
            if (err) throw err;
            console.log("1 record inserted");
        });


    });

  });
});

if I try this simple code, everything works fine:

io.on("connection", function(socket){
console.log("a user is connected " + socket.id );


    socket.on("question", function (question){

        let sql = "INSERT INTO nodeJs (name) VALUES ('John')";

        con.query(sql, function (err) {
            if (err) throw err;
            console.log("1 record inserted");
        });

    });

 });
});

The question parameter always has a string.

like image 876
Markos Avatar asked May 04 '26 22:05

Markos


1 Answers

You're missing the parentheses around the values:

let sql = "INSERT INTO nodeJs (name) VALUES (?)";
// Here ------------------------------------^-^
like image 134
Mureinik Avatar answered May 06 '26 11:05

Mureinik