简体   繁体   中英

MySQL TIMESTAMP not updating when inserting through PHP

So I have a column in MySQL though PHPMyAdmin that looks like this

PHPMyAdmin拍摄

When I insert through PHPMyAdmin into the table and not put anything in the time column it works and puts the time in, however when I do it through PHP with the following script

mysql_query("INSERT INTO `products` 

     VALUES ('', '$title', '', '$photo', '$price', '$details', '$description_short', '$description', '$dimensions', '$colour', '$scatid', '$type', '$brand', 'Y', '$size')") ;  

(the third '' is the time column) it seems to put the timestamp as 0000-00-00 00:00:00

I have had a look around and seem to be doing everything right from looking through some other Stack Overflow posts.

Thank you in advance for your help

Because you have putted it as blank ''

If you want it to be current timestamp then the query should be

INSERT INTO products(list of column_name except time column_name) values (values of list of columns present in column list)

You can also use NOW() for inserting date and time in database

可能不使用''可能是有问题的使用

NOW()

You are inserting a blank (0) value inside the timestamp column.

You can:

1 - modify your query to exclude the timestamp column:

insert into (foo, bar) values ("val1", "val2")

2 - replace "" with CURRENT_TIMESTAMP at the third position inside values

You actually have 2 options:

option 1:

explicitly use "CURRENT_TIMESTAMP" (or "NOW()" as others have said), like this:

"INSERT INTO `products` VALUES ('', '$title', 'CURRENT_TIMESTAMP', '$photo', '$price', '$details', '$description_short', '$description', '$dimensions', '$colour', '$scatid', '$type', '$brand', 'Y', '$size')"

option 2:

implicitly use the default value. To use this, you need to specify the columns you're inserting (between the table name "products" and the keyword VALUES), skipping the timestamp column both in the column list and values list.

I personally prefer option 2 for debug purposes (you can instantly see which columns are for which values and viceversa without looking at the db schema)

尝试将默认值设置为CURRENT_TIMESTAMP而不是将其放在属性中。

使用NOW()mysql命令作为timestamp列的值

mysql_query("INSERT INTO `products` VALUES ('', '$title', NOW(), '$photo', '$price', '$details', '$description_short', '$description', '$dimensions', '$colour', '$scatid', '$type', '$brand', 'Y', '$size')") ; 

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