Lamez Posted January 13, 2008 Share Posted January 13, 2008 how do I set a default value in MySQL? or using a PHP script? thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/ Share on other sites More sharing options...
phpQuestioner Posted January 13, 2008 Share Posted January 13, 2008 What do you want to set a default value for? Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-437838 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 I have a avatar field under a table I want to set a value for, so if they do not select a avatar, it shows a default one. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-437843 Share on other sites More sharing options...
phpQuestioner Posted January 13, 2008 Share Posted January 13, 2008 you could do something like this: <?php if ($avatartype == NULL) { $avatartype="default_avatar.jpg"; // to whatever you want your default to be set to } echo "<img src=\"$avatartype\">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-437847 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-437958 Share on other sites More sharing options...
trq Posted January 13, 2008 Share Posted January 13, 2008 CREATE TABLE foo ( data VARCHAR(80) DEFAULT 'blah blah' ); Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-437986 Share on other sites More sharing options...
nikefido Posted January 13, 2008 Share Posted January 13, 2008 it's better to set the default in your database rather than use PHP for it. It's both faster and more secure. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438044 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 right, I could use a query right? but how would I alter a existing row inside a table? table = users row = avatar -I am not too sure, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438194 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 Use the UPDATE mysql function Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438196 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 alright, I will have to look that up, thanks Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438203 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 well what about the alter table? would this work? ALTER TABLE `users` CHANGE `avatar` `avatar` VARCHAR( 50 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT 'http://www.lamezz.info/user/userava/default.jpeg' Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438210 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 That's fine if you want to change the Table to have a default value, then you would use Alter. But if you want to UPDATE an existing row/record, then you need to use the UPDATE function. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438232 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 well, I am not too sure if default is they way I need to go I want a default avatar if they do not set one, what would the best way to go about this? Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438248 Share on other sites More sharing options...
revraz Posted January 13, 2008 Share Posted January 13, 2008 If you want something in the DB as a default if nothing is entered, then yes, set the Table to have a default for that column. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438249 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 alright I did that, I went in my DB using Phpmyadmin, and set the default value to the image I wanted, then I set it to "not null", and when I register a new user, it does not show the default user, then I look in the DB and the avatar is not set. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438259 Share on other sites More sharing options...
Lamez Posted January 13, 2008 Author Share Posted January 13, 2008 :-\ Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438325 Share on other sites More sharing options...
Fyorl Posted January 13, 2008 Share Posted January 13, 2008 When you insert the user's row into the table you need to make sure you leave the avatar field blank so that the default value gets used. Quote Link to comment https://forums.phpfreaks.com/topic/85787-default-value/#findComment-438341 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.