SauloA Posted January 24, 2007 Share Posted January 24, 2007 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 ; Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/ Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 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 ; Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168351 Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 I've already created the table so how would the code look when using ALTER? Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168369 Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 Changing my code to:`urev_date` datetime NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,isn't working. I get an error in phpMyAdmin saying that the code isn't invalid.Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168414 Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 try taking the NOT NULL option off.... that might work, i know it does in Query Browser Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168420 Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 I removed NOT NULL but I still get an error that the code is invalid. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168435 Share on other sites More sharing options...
dgiberson Posted January 24, 2007 Share Posted January 24, 2007 If there's not a whole lot of data in the table, I would suggest scrapping the table and start from the beginning, if there is a bunch of data, I can check phpMyAdmin once I get home... Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168439 Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168448 Share on other sites More sharing options...
Daney11 Posted January 24, 2007 Share Posted January 24, 2007 This may be of some help.http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168564 Share on other sites More sharing options...
SauloA Posted January 24, 2007 Author Share Posted January 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168568 Share on other sites More sharing options...
Daney11 Posted January 24, 2007 Share Posted January 24, 2007 Hmm this one?http://dev.mysql.com/doc/refman/5.0/en/timestamp-4-1.html Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168569 Share on other sites More sharing options...
SauloA Posted January 25, 2007 Author Share Posted January 25, 2007 I don't want to use a TIMESTAMP field. I've looked at that MySQL documentation you have been reading many times when I was learining MySQL. I don't think that's the best resource. Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-168580 Share on other sites More sharing options...
dgiberson Posted January 25, 2007 Share Posted January 25, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/35558-cant-make-datetime-fields-default-value-now-help/#findComment-169009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.