[英]Current date and time is not inserting in table by this query
var currentDate=new Date().toLocaleString().slice(0,10);
var now = new Date().toLocaleTimeString();
if(data = true){
con.query('INSERT INTO rooms (user_id,current_date,current_time) VALUES (?,?,?)',[user_id,currentDate,now],function(error,results,fields){
if(error) throw error;
发生此错误:MySQL服务器版本,用于在'current_date,current_time附近使用正确的语法
我不知道导致错误的确切原因,但是MySQL的日期/时间API实际上提供了可用于访问当前日期和时间的函数。 所以以下应该在这里工作:
var sql = 'INSERT INTO rooms (user_id, `current_date`, `current_time`) ';
sql += 'VALUES (?, CURRENT_DATE, CURRENT_TIME)';
con.query(sql, [user_id], function(error, results, fields) {
if(error) throw error;
});
附带说明一下,为什么将日期和时间存储在单独的列中? 通常只有在有非常令人信服的理由时才应这样做。 否则,只需将日期和时间一起存储在时间戳或datetime列中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.