开发者

MySQL Insert Into datetime = NOW() is not working? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that 开发者_Go百科is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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.


How about:

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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜