尝试使用Javascript和NodeJS在XAMPP本地主机上更新MySQL中的数据库表,但是由于某些原因,代码未按我认为的方式处理。这是一个时钟程序,我所要做的只是一个简单的测试,即单击按钮时将名称插入到列中。基本上,该部分代码到达时不执行任何操作(它仅在第一个按钮上)。我该怎么办?谢谢。
JS
var clock = {
bool: false,
in: 0,
inText: "Clock-in time: ",
inText2: "Already clocked in: ",
out: 0,
outText: "Not clocked in.",
outText2: "Clock-out time is: "
};
var rest = {
bool: false,
out: 0,
outText: "Break-out time is: ",
outText2: "Already on break: ",
in: 0,
inText: "Not on break.",
inText2: "Break-in time is: "
};
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "mydb"
});
function clockIn(id) {
switch (clock.bool) {
case false:
var d = new Date();
var t = d.toLocaleTimeString();
clock.bool = true;
clock.in = t;
document.getElementById(id).innerHTML = clock.inText + clock.in;
con.connect(function (err) {
if (err)
throw err;
console.log("Connected!");
var sql = "INSERT INTO timeclock (Name) VALUES ('Name')";
con.query(sql, function (err, result) {
if (err)
throw err;
console.log("1 record inserted");
});
});
break;
case true:
document.getElementById(id).innerHTML = clock.inText2 + clock.in;
break;
}
}
function clockOut(id) {
switch (clock.bool) {
case false:
document.getElementById(id).innerHTML = clock.outText;
break;
case true:
var d = new Date();
var t = d.toLocaleTimeString();
clock.bool = false;
clock.out = t;
document.getElementById(id).innerHTML = clock.outText2 + clock.out;
break;
}
}
function breakOut(id) {
switch (rest.bool) {
case false:
var d = new Date();
var t = d.toLocaleTimeString();
rest.bool = true;
rest.out = t;
document.getElementById(id).innerHTML = rest.outText + rest.out;
break;
case true:
document.getElementById(id).innerHTML = rest.outText2 + rest.out;
break;
}
}
function breakIn(id) {
switch (rest.bool) {
case false:
document.getElementById(id).innerHTML = rest.inText;
break;
case true:
var d = new Date();
var t = d.toLocaleTimeString();
rest.bool = false;
rest.in = t;
document.getElementById(id).innerHTML = rest.inText2 + rest.in;
break;
}
}
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("time").innerHTML = "Current time is: " + t;
}
function resetBtnTxt(id, text) {
setTimeout(function () {
document.getElementById(id).innerHTML = text;
}, 5000);
}