php MySQL Insert Into datetime = NOW() 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4548637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Insert Into datetime = NOW() is not working?
提问by JM4
I have the following code (php, mysql, pdo):
我有以下代码(php、mysql、pdo):
$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = NOW()");
$stmt->execute(array($party));
when run, the party is insert correctly but the date is not inserting as it should (the system date and time at action). I have verified numerous times the field type for date is datetime.
运行时,聚会正确插入,但日期未按应有的方式插入(系统日期和时间在行动)。我已经多次验证日期的字段类型是日期时间。
Any ideas?
有任何想法吗?
EDIT
编辑
To give actual data and the results returned:
给出实际数据和返回的结果:
assume the following:
假设如下:
$party = 'John';
$party = '约翰';
the results return:
结果返回:
party | date
-------------------------------------
John | 0000-00-00 00:00:00
update:
更新:
When i run the following code directly within a mysql query browser, the insert works just as it should:
当我直接在 mysql 查询浏览器中运行以下代码时,插入工作正常:
insert into agent_temp set party = 'John', date = NOW();
插入 agent_temp 集合 party = 'John', date = NOW();
returning:
返回:
party | date
-------------------------------------
John | 2010-12-28 13:15:23
ANSWERED
回答
Well, who is ready to kill me? I have no idea what caught it up but unfortunately the issue seemingly was due to an earlier version of the php script from my machine being cached and still running bad data. I refreshed, closed, and emptied my browser and now the script works. My apologies for making everybody's brains melt just a little.
好吧,谁准备杀了我?我不知道是什么引起了它,但不幸的是,这个问题似乎是由于我的机器上的早期版本的 php 脚本被缓存并且仍在运行错误数据。我刷新、关闭并清空了浏览器,现在脚本可以工作了。我很抱歉让每个人的大脑都融化了一点。
回答by Johandk
How about:
怎么样:
$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = ?");
$stmt->execute(array($_POST['party'], date("Y-m-d H:i:s")));
回答by Quassnoi
This is a pure INSERT
statement, it does not update any rows.
这是一个纯INSERT
语句,它不会更新任何行。
回答by Inca
Untested but perhaps the problem is that 'date' is a reserved word, you could try renaming your column and see if it works.
未经测试,但也许问题在于“日期”是一个保留字,您可以尝试重命名您的列,看看它是否有效。
The other approach is to add a timestamp-field with an 'ON UPDATE CURRENT_TIMESTAMP'.
另一种方法是添加一个带有“ON UPDATE CURRENT_TIMESTAMP”的时间戳字段。
回答by Scoobler
I'm not sure which system you are using, but I would think your prepare statement will be adding quotation around the NOW()
part - causing the statement to try and insert NOW()instead of running the mysql function NOW()
- thus because the field can't store the characters NOW()you get the 000-00.....
我不确定您使用的是哪个系统,但我认为您的准备语句将在该NOW()
部分周围添加引号- 导致语句尝试插入NOW()而不是运行 mysql 函数NOW()
- 因此因为该字段不能存储字符NOW()你得到 000-00 .....
Out of interest, try changing the field type from DATETIME
to TEXT
and see what you get when you run the command.
出于兴趣,尝试将字段类型从 更改DATETIME
为TEXT
,看看运行命令时会得到什么。