使用MySQL / PHP在现场使用now()插入当前日期/时间 [英] Insert current date/time using now() in a field using MySQL/PHP

查看:86
本文介绍了使用MySQL / PHP在现场使用now()插入当前日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于MySQL显然无法在datetime字段中自动插入函数now(),所以在添加新记录的同时,基于注释,我明确地尝试使用SQL语句来插入它。 (人们似乎认为时间戳与curdate()不是由于时间戳的各种限制的答案。)网络上有许多文章建议使用SQL插入now()应该工作。



然而,当我尝试使用SQL语句插入日期时间时,该字段不会填充当前的时间/日期,但它只给我默认的0000-00-等。这可能是一个语法错误,但它让我疯狂,所以我发布它。

  mysql_query(INSERT INTO users(first,last,whenadded)VALUES('$ first','$ last' ()); 

它插入第一个和最后一个,但没有添加,留下0000-00 -00等。在



字段类型是datetime,没有排序规则,属性,默认或额外的BTW,我试着放了)单引号...它抛出一个错误。

解决方案

NOW()通常在SQL语句中工作,并返回日期和检查您的数据库字段是否具有正确的类型(datetime),否则您可以随时使用 PHP date()函数并插入:

  date('Ymd H:i:s')

但我不会推荐这个。


Since MySQL evidently cannot automatically insert the function now() in a datetime field in adding new records like some other databases, based on comments, I'm explicitly trying to insert it using an SQL statement. (People seem to think timestamp with curdate() is not the answer due to the various limitations of timestamp.) There are numerous articles on the web suggesting inserting now() using SQL should work.

When I try to insert the date time using the SQL statement, however, the field does not populate with the current time/date, but it only gives me the default 0000-00-, etc. This is probably a syntax error, but it's driving me crazy, so I am posting it.

mysql_query("INSERT INTO users (first, last, whenadded) VALUES ('$first', '$last', now())"; 

It inserts first and last, but nothing for when added, leaving 0000-00-00, etc. in the whenadded field.

The field type is datetime, it has no collation, attributes, null default or extra. BTW, I tried putting now() in single quotes... It threw an error.

解决方案

NOW() normally works in SQL statements and returns the date and time. Check if your database field has the correct type (datetime). Otherwise, you can always use the PHP date() function and insert:

date('Y-m-d H:i:s')

But I wouldn't recommend this.

这篇关于使用MySQL / PHP在现场使用now()插入当前日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆