简体   繁体   中英

TIMESTAMP on UPDATE doesn't work with PHP

I have a feeddate column with the info below..

type : timestamp
attributes : on update CURRENT_TIMESTAMP
default : CURRENT_TIMESTAMP

and when I used PHP to INSERT INTO the rows with this code ..

$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','','')";
$badgefeedQ = mysql_query($badgefeed);

(feeddate is on the last column that NULL)

This feeddate doesn't update and be like 0000-00-00 00:00:00
but it's gonna work when I used PHP to UPDATE something that already had in the table.

Did I do anything wrong with my feeddate structure or the INSERT code is incorrect ?

Your query should be:

$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','')";

Don't use any value for the last column as you are using the timestamp as default value in MySQL. Just omit the last (blank) value from your query. In such a way, value for the concerned column will be considered null and thus default timesamp will will be used.

Insead of '' use null .
This way, mysql manages the value itself.

If you specify any value, the server will take your value, regardless other triggers, settings.

If you don't include the column in the insert statement or leave it blank (null), it will get set by the server, according to the defaults. In this case the default being on update CURRENT_TIMESTAMP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM