1

How do I make the below code less vulnerable to SQL injection attacks? and also be able to accept " and ' characters in the parameters?

app.get('/addStudent',(req,res) => {
  const {fname, lname, othname, bloodType}= req.query;

  let sqlstmt = "INSERT INTO `students` (`fname`, `lname`, `othname`, `bloodtype`) VALUES ('"+fname+"', '"+lname+"', '"+othname+"', '"+bloodType+"')"
    db.query(sqlstmt,(err,result) => {
    if(err){console.log('Error occured while fetching user information',err)}
    console.log(result);
    res.send(result);
  });
});
0

1 Answer 1

2

You should use prepared statements which should be supported by your driver package for your chosen DBMS.

An example for MySQL: https://github.com/mysqljs/mysql#preparing-queries

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but more to the point, use query parameters. Just doing a prepared statement from a string that has been formed with string-concatenation doesn't magically make it safe.
how can I modify my above code using query parameters and prepared statements?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.