PHP mysql auto insert timestamp
38,695
Solution 1
What you need to do is declare timestamp
to be of type in your sql
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
and modify the query to
$query = "insert into auto_parts(id, part, price)
values(1, 'axle', 200)"
mysql_query($query);
Solution 2
$query = "insert into auto_parts(id, part, price, timestamp)
values(1, 'axle', 200, 'NOW()')"
mysql_query($query);
Solution 3
Do it in SQL itself instead of passing it ... its more efficient ... This is the corresponding post:
Auto TimeStamp new entry to DB (phpMyAdmin)
Author by
user1058275
Updated on July 09, 2022Comments
-
user1058275 almost 2 years
Say I have a table name auto_parts with these fields.
id, part, price, timestamp
and I insert a new row via php as so:$query = "insert into auto_parts(id, part, price, timestamp) values(1, 'axle', 200)" mysql_query($query);
will that automatically add the timestamp. Or do I have to insert a value for timestamp myself?
-
k.honsali about 12 yearsyou can use only one timestamp variable per table !!
-
Sven Viking about 11 yearsOnly one automatic timestamp per table, whether via DEFAULT or UPDATE (though I think that's changed in recent MySQL versions). You can have multiple timestamp columns, as long as no more than one is set/updated automatically. dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
-
Romain about 10 yearsIt is convenient when you have more than 1
timestamp
column (and one is automatic, see Sven Viking comment for details) and want to update/insert a new data in one other timestamp column.