MySQL Insert Into datetime = NOW() is not working? [closed]
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'.
精彩评论