MySQL Timestamp - why all zeros?
-
10-07-2019 - |
Question
I'm using PHPMyAdmin and I've got a MySQL table column called "timestamp." The type (surprise!) is TIMESTAMP
, and in 'attributes' I've set it to ON UPDATE CURRENT_TIMESTAMP
.
However, each new record gets a timestamp that looks like this:
0000-00-00 00:00:00
I have explicitly set the default value to none, but when I save and come back to look, it is set to all zeros as above.
The relevant PHP records page hits with this query:
$query = "INSERT INTO `pagehit` (user_id, pageurl)
VALUES ('" . $userid . "', '" . $pageurl . "')";
The whole thing is running under XAMPP.
What am I missing?
Solution
What am I missing?
You don't update :)
Use DEFAULT CURRENT_TIMESTAMP
along with ON UPDATE CURRENT_TIMESTAMP
OTHER TIPS
Try setting the default value to CURRENT_TIMESTAMP
instead of putting that in the attributes.
If your timestamp
column captures only the insertion time then use only
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Otherwise if it is for modification time then use like as follows
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
In my case works like this:
In phpMyAdmin:
ALTER TABLE `name_table` ADD `name_row` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
In PHP sintax for the row:
date('Y-m-d H:i:s')