1

Iam new in nodejs, i have a query regarding inserting values in mysql.i have made connection with mysql and now my requirement is to fetch data from query url which i have already done by node js parser.By below code-:

app.get('/insertData',function(req,res){

  var url_parts = url.parse(req.url, true);
var query = url_parts.query;
**var id** = req.query.id;
console.log(id);
connection.query("insert into otps (from_user_id,to_user_id,otp,is_company,status,created_on) values (**'id'**,'2345','1','2','2018-04-04 00:00:00')",function(err,result){

if(!!err){
console.log(err);
res.send('Error in inserting');
}
else{

  res.send('Successfully Insertion');
}});});

The problem is when iam sending that parse value (id) to my sql table,then null is getting inserted in the table.So how can i save a value from query url to db.

Query Url=http://localhost:1337/insertData?id=20

Thanks enter code here

3
  • is it showing id in console ? use id without quotes in query Commented Apr 4, 2018 at 12:07
  • A better way to debug what is happening, is to build your query string before you send it through, then log in console the query and test directly in SQL. I won't even mention the security problems of allowing direct insertion like this. Commented Apr 4, 2018 at 12:08
  • it's showing id=20 in console,but when i'm sending it to table ,0 is getting inserted there. Commented Apr 4, 2018 at 12:19

1 Answer 1

1

you are giving id as string .remove single inverted commas ('') in query.

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sql = "CREATE TABLE mydb4 (from_user_id int, to_user_id int)";
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Table created");
  });
});
app.get('/insertData',function(req,res){

  var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var id = req.query.id;
console.log(id);
con.connect(function(err) {
con.query("insert into mydb4 (from_user_id,to_user_id) values ("+id+",2345)",function(err,result){

if(!!err){
console.log(err);
res.send('Error in inserting');
}
else{
 con.query("SELECT * FROM mydb4", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
  res.send('Successfully Insertion');
}});});});
Sign up to request clarification or add additional context in comments.

6 Comments

Hi thanx for your answer but doing this 0 is going in table and on printing same ' id' on console iam getting 20. for localhost:1337/insertData?id=20
what is the datatype u have given for from_user_id
datatype for from_user_id is 'int'
edited..its working for me..can u execute above code and let me know the result
Thanx ..it's working now ,i think it's the issue of "" in "+id+".
|

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.