所以我想使用nodejs将javascript变量推送到mysql中。我尝试使用以下代码:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "xxx.xxx.xxx.xxx",
user: "loginUSER",
password: "xxxxx",
database: "scoutingData"
});
function databaseCon() {
console.log("submit works and connected.");
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO scoutingDataTab (compId, teleop_cargoTot, teleop_hatchTot) VALUES (compId, teleop_noOfCargo, teleop_noOfHatch)";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
}
这里,变量设置在javascript的顶部,databasecon()函数从submit按钮调用。
compId, teleop_cargoTot, teleop_hatchTot
是数据库中的列和
compId, teleop_noOfCargo, teleop_noOfHatch
是js中的变量。我的web服务器上下载了node.js。我试着点击提交,一个错误弹出,但页面重新加载太快。我该怎么解决?
提前谢谢!