PHP mysql auto insert timestamp

后端 未结 4 1673
萌比男神i
萌比男神i 2021-02-14 02:32

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_p         


        
4条回答
  •  执笔经年
    2021-02-14 02:58

    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,

    1. Change your table and make timestamp column to 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)"; mysql_query($query);

    1. If you have more than one timestamp table then you can make one as current timestamp and for other one use like this

    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())"; mysql_query($query);

提交回复
热议问题