0

I have a problem to send a mysql query where this problem :ER_PARSE_ERROR: You have an error in your SQL syntax appears, my code where user is an javascript object contains 3 strings username,email,password .

let sql2 = 'insert into users (username,email,password) values (' + user.username + ',' + user.email + ',' + user.password + ')'  //query

           
        db.query(sql2,(err,result) => {
                if(err) throw err 
                res.send('User registriation done')
        })
4
  • you need to use escaping arround every injected variable. use db.escape() for exemple. Commented Aug 23, 2021 at 21:26
  • Use parameters instead of concatenating variables. Commented Aug 23, 2021 at 21:27
  • @Barmar if i need to use this query how to write user.emailcorrect : let sql = "select email from users where email = " + user.email Commented Aug 23, 2021 at 22:00
  • let sql = 'select email from users where email = ?' and then pass user.email in the parameter arraay. Commented Aug 23, 2021 at 22:02

1 Answer 1

2

You're not putting quotes around the string values. But the correct way is to use parameters rather than concatenating variables into the SQL.

let sql2 = 'insert into users (username,email,password) values (?, ?, ?)' //query
db.query(sql2, [user.username, user.email, user.password], (err, result) => {
  if (err) throw err
  res.send('User registriation done')
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.