2013-04-29 10:16
浏览 211


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

PHPMyAdmin shot

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • duangou1953
    duangou1953 2013-04-29 10:22

    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)

    点赞 评论
  • donglei3370
    donglei3370 2013-04-29 10:18

    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

    点赞 评论
  • dt56449492
    dt56449492 2013-04-29 10:19

    May be don't use '' may be a problem use

    点赞 评论
  • dongmin3754
    dongmin3754 2013-04-29 10:22

    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

    点赞 评论
  • duanchique1196
    duanchique1196 2013-04-29 10:24

    Try setting the default value to CURRENT_TIMESTAMP instead of putting that in the attributes.

    点赞 评论
  • douqiao1413
    douqiao1413 2013-04-29 10:29

    Use NOW() mysql command as value for timestamp column

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