AppleTattooGuy
AppleTattooGuy

Reputation: 1175

MySQL TIMESTAMP not updating when inserting through PHP

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

Upvotes: 1

Views: 1596

Answers (6)

LombaX
LombaX

Reputation: 17374

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

Upvotes: 1

BestDeveloper
BestDeveloper

Reputation: 343

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')") ; 

Upvotes: 1

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

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

Upvotes: 1

STT LCU
STT LCU

Reputation: 4330

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)

Upvotes: 1

chandresh_cool
chandresh_cool

Reputation: 11830

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

NOW()

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

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

Upvotes: 2

Related Questions