I think I may have encountered a bug in mysql, or is it just me doing it wrong.
I’ve been using the same specific queries for the last four months and just today it stopped working somehow. I can’t see the problem.
I’m executing these queries in the mysql console it works great, and the field is being updated. but when these queries are being executed by PHP it fails.
After insertion of a record into a table(with two timestamp fields), I’m trying to update a specific timestamp column.
But unfortunately it fails to update the column.
The query goes well(no errors), but still the value in the timestamp column stays the same. That’s weird, cause when I’m leaving the initial column value as NULL, the update query succeed.
Columns :
START_DATETIME, END_DATETIME - are "timestamp" type.
Insert:
INSERT INTO TABLE1(START_DATETIME, END_DATETIME, RESPONSE)
VALUES(NOW(), NOW(), 'STARTED')
Insert is done successfully. id is 123
The update query is normal like any other query:
UPDATE TABLE1
SET END_DATETIME = NOW(), RESPONSE='ENDED'
WHERE ID = 123
Update fails, END_DATETIME doesn’t get the NOW() value.
Can be reproduced with this:
CREATE TABLE TABLE1
(
id int auto_increment,
start_datetime timestamp,
end_datetime timestamp,
response varchar(100),
primary key(id)
);
You probably have defined the first timestamp column (the
START_DATETIMEone) to be auto-inserted and auto-updated with theCURRENT_TIMESTAMPvalue (which is the same asNOW().Notice that if you don’t explicitedly state anything about the
TIMESTAMPcolumns in theCREATE TABLEscript, the first one of them gets by default this behaviour/attributes. Read the MySQL documentation about this Automatic Initialization and Updating forTIMESTAMP, where it states:So, if you do a
SHOW CREATE TABLE tableName, you’ll have something like this:You should alter the column definition to not be auto_updated, if you don’t want this behaviour: