Jump to content

Can't make datetime field's default value NOW(), HELP.


SauloA

Recommended Posts

I have a MySQL database and I'm trying to set my datetime fields have a default value of NOW() or CURDATE or something that records the current date/time.  I'm using phpmyadmin and input NOW() and a number of other default values in the default value text box, but I keep getting an error saying that my default value is invalid.  Is there any way to set a default value that records the current date/time on phpMyAdmin or MySQL?  I'd appreciate the help.

Here's my table structure.  The [color=red]red[/color] is the datetime field I'm trying to change.

CREATE TABLE `user_review_tbl` (
 `urev_id` int(11) NOT NULL auto_increment,
 `user_id` int(11) NOT NULL,
 `urev_review` text NOT NULL,
 [color=red]`urev_date` datetime NOT NULL,[/color]
 `urev_approved` tinyint(1) default NULL,
 `shop_id` int(11) NOT NULL,
 PRIMARY KEY  (`urev_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
Link to comment
Share on other sites

change to:
CREATE TABLE `user_review_tbl` (
  `urev_id` int(11) NOT NULL auto_increment,
  `user_id` int(11) NOT NULL,
  `urev_review` text NOT NULL,
  `urev_date` datetime NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `urev_approved` tinyint(1) default NULL,
  `shop_id` int(11) NOT NULL,
  PRIMARY KEY  (`urev_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
Link to comment
Share on other sites

I've been creating and recreating through the process.  But I may be putting the wrong attributes.

I've written the code exactly like this:

'urev_date` datetime NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

and without the NOT NULL like this:

'urev_date` datetime default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

And I also tried making the default NOW() along with other default values without NOT NULL.

Any ideas?
Link to comment
Share on other sites

The website you provided may be good for formatting time but doesn't seem to be good for creating default values.  Unless you know how to manipulate the data on that website to get me the current date and time displayed then I'm out of luck.
Link to comment
Share on other sites

SauloA, I think for what you are trying to accomplish, a timestamp field is the way you want to go if you want the date/time updated every time the record is touched.... otherwise use an integer(11) field to store unix timestamp, and update that from the php code. i cant find any documentation on setting a default time value without using a timestamp data type...
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.