MySQL 插入 datetime = NOW() 不起作用?

发布于 2024-10-09 15:23:00 字数 860 浏览 1 评论 0原文

我有以下代码(php、mysql、pdo):

$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = NOW()");
$stmt->execute(array($party));

运行时,该方已正确插入,但日期未按应有的方式插入(系统日期和时间)。我已经多次验证日期的字段类型是日期时间。

有什么想法吗?

编辑

要给出实际数据和返回的结果:

假设以下内容:

$party = 'John';

结果返回:

party      |      date    
-------------------------------------
John       |    0000-00-00 00:00:00

更新:

当我直接在 mysql 查询浏览器中运行以下代码时,插入工作正常:

insert into agent_temp set party = 'John', date = NOW();

返回:

party      |      date    
-------------------------------------
John       |    2010-12-28 13:15:23

已回答

那么,谁准备好杀我了?我不知道是什么引起了这个问题,但不幸的是,这个问题似乎是由于我的机器上的早期版本的 php 脚本被缓存并且仍在运行错误数据。我刷新、关闭并清空浏览器,现在脚本可以运行了。我很抱歉让每个人的大脑都融化了一点。

I have the following code (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';

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:

insert into agent_temp set 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薆情海 2024-10-16 15:23:00

怎么样:

$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = ?");
$stmt->execute(array($_POST['party'], date("Y-m-d H:i:s")));

How about:

$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = ?");
$stmt->execute(array($_POST['party'], date("Y-m-d H:i:s")));
草莓味的萝莉 2024-10-16 15:23:00

我不确定您使用的是哪个系统,但我认为您的准备语句将在 NOW() 部分周围添加引号 - 导致语句尝试插入 NOW()< /em> 而不是运行 mysql 函数 NOW() - 因此,因为该字段无法存储字符 NOW() 你得到 000-00.... 出于兴趣,

尝试将字段类型从 DATETIME 更改为 TEXT 并查看运行命令时会得到什么。

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.....

Out of interest, try changing the field type from DATETIME to TEXT and see what you get when you run the command.

苦行僧 2024-10-16 15:23:00

这是一个纯粹的 INSERT 语句,它不会更新任何行。

This is a pure INSERT statement, it does not update any rows.

北陌 2024-10-16 15:23:00

未经测试,但问题可能是“日期”是保留字,您可以尝试重命名您的列,看看它是否有效。

另一种方法是添加带有“ON UPDATE CURRENT_TIMESTAMP”的时间戳字段。

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文