Jump to content

Having problems with dates


jiveturkey420

Recommended Posts

I was creating a news section to a website, and ran into a problem.

I am using a MySQL database to store the news entrys, and this particular table only has 2 columns. I created the table like so....

 

CREATE TABLE news (time timestamp NOT NULL default now(), news varchar(10000));

 

The news is inserted into the table using like so......

 

INSERT INTO news VALUES (' ', '$newnews');

 

...through a php script.

 

The value of the first column of my table always winds up being  0000-00-00  00:00:00

 

Why isn't it recording the proper time?

I am using a LAMP stack on Fedora 17

 

 

Link to comment
Share on other sites

You don't want a space in there.

 

INSERT INTO news VALUES ('', '$newnews');

 

You can't use NOW() for a default value either (this may have changed with the latest version though). From what I know about MySQL5, default must be a constant value. Leave default blank, and use this instead.

 

INSERT INTO news VALUES (NOW(), '$newnews');

Link to comment
Share on other sites

You can't use "NOW()" as the default, but you can use "CURRENT_TIMESTAMP"

 

CREATE TABLE news (time timestamp NOT NULL default CURRENT_TIMESTAMP, news varchar(10000));

 

But, if you are going to have a field with a default, what's the point of having to include an empty value when creating the record? Just don't include the field in the insert. Include the field list that you are inserting for and the associated values.

INSERT INTO news (news) VALUES ('$newnews');

 

And, for what it's worth, I would suggest not having a field name and a table name with the same value. Only creates confusion in my opinion.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.