To insert a value passed by parameter in the sql query

Asked

Viewed 102 times

0

I am passing two values of an input to the function "Insert", the values then arriving at the function normally, I am printing them to confirm, but still I can not insert the values passed in the table.

Returns no error, simply does not insert the values.

insert(connection, email, password) {
    console.log(email)
    console.log(password)
    var sql = `INSERT INTO register (email, password) VALUES (${email}, ${password})`
    connection.query(sql, function (err, result) {
        if (err) {
            return ('Falha ao inserir dados!' + err)
        }
        console.log('Dados inseridos com sucesso!')
    })
}

1 answer

1

You could try using native database bindings?

The result would be:

var sql = `INSERT INTO register (email, password) VALUES (?, ?)`
    connection.query(sql, [email, password], function (err, result) {
        if (err) {
            return ('Falha ao inserir dados!' + err)
        }
        console.log('Dados inseridos com sucesso!')
    })
  • God, it was killing me and it was so simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.