【发布时间】:2023-03-27 09:46:02
【问题描述】:
我需要创建一份报告,生成一份按日期细分的教师和访问实验室的学生列表。
但目前我正在使用 getTime 函数添加签入和签出时间,并将该时间保存为 MySQL 中的 VARCHAR,因为我在 MySQL 中的时间戳存在问题,并且每次签入/签出都是它自己的行。我还担心我将无法使用这些数据来正确生成报告。
当某人签入一行时,我如何才能创建它,当他们稍后签出时,他们的前一行会使用当前日期时间更新为 checkOut?
// Checking In/Out users
router.post("/checkin", (req, res) => {
const id = req.body.create_id
const firstName = req.body.create_first_name
const lastName = req.body.create_last_name
const checkIn = getDateTime();
const checkOut = getDateTime();
console.log("Checking In...")
console.log("ID: " + req.body.create_id)
console.log("First Name: " + req.body.create_first_name)
console.log("Last Name: " + req.body.create_last_name)
console.log(checkIn)
console.log(checkOut)
// Checking Student In
if(req.body.person === "Student" && req.body.checkInOut === "checkIn") {
queryString = "INSERT INTO students ( student_id ,firstName, lastName, checkIn) VALUES(?, ?, ?, ?)"
getConnection().query(queryString, [id, firstName, lastName, checkIn], (err, rows, fields) => {
// If error occures
if(err) {
console.log("Failed to check in new user: " + err)
res.sendStatus(500)
return
}
console.log("Checked in new user with id: ", id)
})
}
// Checking Student Out
else if(req.body.person === "Student" && req.body.checkInOut === "checkOut") {
queryString = "INSERT INTO students ( student_id ,firstName, lastName, checkOut) VALUES(?, ?, ?, ?)"
getConnection().query(queryString, [id, firstName, lastName, checkOut], (err, rows, fields) => {
// If error occures
if(err) {
console.log("Failed to check out new user: " + err)
res.sendStatus(500)
return
}
console.log("Checked out user with id: ", id)
})
}
【问题讨论】:
标签: javascript mysql node.js ejs