[英]PHP mysql auto insert timestamp
Say I have a table name auto_parts with these fields. 假设我有一个包含这些字段的表名auto_parts。 id, part, price, timestamp
and I insert a new row via php as so: id, part, price, timestamp
和我通过php插入一个新行如下:
$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? 或者我是否必须自己插入时间戳值?
What you need to do is declare timestamp
to be of type in your sql 您需要做的是将timestamp
声明为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);
$query = "insert into auto_parts(id, part, price, timestamp)
values(1, 'axle', 200, 'NOW()')"
mysql_query($query);
Do it in SQL itself instead of passing it ... its more efficient ... This is the corresponding post: 在SQL本身做它而不是传递它...它更有效...这是相应的帖子:
Auto TimeStamp new entry to DB (phpMyAdmin) 自动TimeStamp DB的新条目(phpMyAdmin)
I know there are already answers to this question so I am kind of combining all answers and explaining a little bit. 我知道这个问题已有答案,所以我将所有答案结合起来并解释一下。 There may be two ways to do this, 可能有两种方法可以做到这一点,
ALTER TABLE tablename MODIFY timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ALTER TABLE tablename MODIFY timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
and modify your query like this, timestamp will be inserted automatically 并像这样修改您的查询,将自动插入时间戳
$query = "insert into auto_parts(id, part, price) values(1, 'axle', 200)"; $ query =“插入auto_parts(id,part,price)值(1,'轴',200)”; mysql_query($query); 的mysql_query($查询);
ALTER TABLE tablename MODIFY timestamp TIMESTAMP NOT NULL ALTER TABLE tablename MODIFY timestamp TIMESTAMP NOT NULL
and modify your query like this 并像这样修改你的查询
$query = "insert into auto_parts(id, part, price, timestamp) values (1, 'axle', 200, now())"; $ query =“插入auto_parts(id,part,price,timestamp)值(1,'轴',200,now())”; mysql_query($query); 的mysql_query($查询);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.