I'm performing CRUD operations for RESTFUL API using MySQL and NodeJS express. There's an error in inserting a record into DB, I don't know why I was getting a problem as I have copied the insert query from SQL where it was working properly. I wrote a query like this var sql = 'INSERT INTO tblltest values(null, "+req.body.Name+")';
. I've tested it using PostMan instead of adding name it adds +req.body.Name+
param into DB. Please help me out to solve this issue.
Here are my lines of code:
var express = require('express');
var mysql = require('mysql');
var app = express();
const bodyparser = require('body-parser');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'sampledb'
});
app.post('/' , function(req , resp) {
var bg = req.body;
var sql = 'INSERT INTO `tblltest` values(null, "+req.body.Name+")';
connection.query(sql , function(error , rows , fields){
if(!error){
console.log('Successful added!! \n');
resp.json(rows);
}else{
console.log('Error in adding');
}
});
})
app.listen(1337);