简体   繁体   English

使用node.js将记录插入MySQL

[英]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. 使用ajax从表单中获取记录以使用node.js插入到mysql中,但是只有第一列有效。 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 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);
  })
});

Do you use https://github.com/felixge/node-mysql ? 您是否使用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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM