Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysql auto insert timestamp

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?

like image 911
user1058275 Avatar asked Dec 29 '11 00:12

user1058275


3 Answers

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);
like image 189
parapura rajkumar Avatar answered Oct 07 '22 13:10

parapura rajkumar


$query = "insert into auto_parts(id, part, price, timestamp)
  values(1, 'axle', 200, 'NOW()')"
mysql_query($query);
like image 30
Ali Demirci Avatar answered Oct 07 '22 14:10

Ali Demirci


Do it in SQL itself instead of passing it ... its more efficient ... This is the corresponding post:

Auto TimeStamp new entry to DB (phpMyAdmin)

like image 25
David Avatar answered Oct 07 '22 15:10

David