Abdul Suadik Mohammed Abdul Suadik Mohammed - 6 months ago 72
Ajax Question

Inserting records into MySQL with node.js

Fetching records from a form with ajax to insert into mysql with node.js but only the first column works. The rest of the columns do not insert. Below is my code. What am I doing wrong? Thanks

Ajax

$(document).ready(function (e) {
$("#new-user-form").on('submit',(function(e) {
e.preventDefault();
var data = {};
data.title = $('#title').val();
data.fname = $('#fname').val();
data.mname = $('#mname').val();
data.lname = $('#lname').val();
data.mdname = $('#mdname').val();
$('#loading').show();
//toastr.success('Page Loaded!');
$.ajax({
url: "/new-user",
type: "POST",
data: data,
dataType: 'application/json',
cache: false,
success: function(data)
{
console.log('working');
$('#loading').hide();
}
});
}));
});


new-user.js

var express = require('express'),
router = express.Router(),
db = require('./../db');

router.post('/', function (req,res,next) {
var title = req.body.title;
var first_name = req.body.fname;
var middle_name = req.body.mname;
var last_name = req.body.lname;
var maiden_name = req.body.mdname;
db.insert({first_name:first_name},{last_name:last_name}).into('users').then(function(data){
//res.send(data);
})
});

Answer

Do you use https://github.com/felixge/node-mysql?

Try pass the object as one:

db.insert({first_name:first_name, last_name:last_name})

Insted of

db.insert({first_name:first_name},{last_name:last_name})