0

Basically I have two table

User -> id, name , email , fname, lname etc... Device -> id, name, user_id, etc...... 

here first I will insert data into User table and I get result, from that result how to get the Id of the User table entry so that I can use is as user_id for the entry in device basically user_id is foreign key referring User table

My insert code goes like this

exports.user = function(req,res){ var user_email = req.param('email', null); var user_fname = req.param('fname', null); var user_lname = req.param('lname', null); var user_phone = req.param('phone', null); var user_description = req.param('description',null); var user_data = { table:TABLE_USER, data:{ 'email':user_email, 'fname':user_fname, 'lname':user_lname, 'phone':user_phone, 'description':user_description } } db.insert(user_data,function (result) { //How to get the ID of the last inserted row from result, // get Id and insert in device table res.writeHeader(200, {"Content-Type": "text/plain"}); res.write(result[0] + " "); res.end(); } ); } 
2
  • What mysql driver/node module do you use? Commented Apr 15, 2015 at 11:27
  • I am using mysql "version": "2.6.2", Commented Apr 15, 2015 at 11:28

1 Answer 1

1

Just have a look at the documentation : Getting the id of an inserted row

Their code example :

connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) { if (err) throw err; console.log(result.insertId); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.