How I am performing a SQL query to insert data into the database. Note that the field PlanDetails in the table dbo.Plans is a text field. However, when a user tries to input a sentence which contains an apostrophe (e.g. I'll go to sleep). via the HTML form . The database returns an error "Incorect syntax near ll), which refers to the apostrophe problem. In this case, how should I modify the code below to make it accept sentences with apostrophe? It is told that I can use parameterized queries. In this node js example, how can I do that?
CreatePlan : function (newPlan , UserID, DisplayName, Email, callback) {
var sql = require('mssql');
var config = require('./configuration/sqlconfig');
var conn = new sql.Connection(config);
var req = new sql.Request(conn);
conn.connect(function (err) {
if (err) {
console.log(err);
return;
}
console.log('Attempting to Insert new learning plan...');
req.query('INSERT INTO dbo.Plans (PlanTitle, PlanTopic, OwnerID, OwnerName, PlanDetails, TargetLearners, OwnerContact) VALUES ("' + newPlan.PlanTitle + '", "' + newPlan.PlanTopic + '", "' + UserID + '", "' + DisplayName + '", "' + newPlan.PlanDetails + '", "' + newPlan.TargetLearners + '", "' +Email+ '");', function (err) {
if (err) {
console.log(err);
} else {
console.log("Added one new learning plan");
console.log("The new plan title is " + newPlan.PlanTitle);
callback();
}
conn.close();
});
});
},