doubledee Posted June 30, 2012 Share Posted June 30, 2012 If a user leaves a Form Field blank, what should I store in my database? A NULL or the Empty String? I am working on Form Validation, and keep switching back on forth on this and am going crazy?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/ Share on other sites More sharing options...
kicken Posted June 30, 2012 Share Posted June 30, 2012 I'd say it depends on what the field is. For text fields like names, urls, emails, etc I generally just let it be an empty string and define the field not null. If the field is storing an ID value which is a reference to another table, or a date/time value, I will set it to NULL when left empty, rather than use a value like 0 or something. Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358204 Share on other sites More sharing options...
darkfreaks Posted June 30, 2012 Share Posted June 30, 2012 i would use NULL if you are checking a field for a database value. if you are checking a string like "asfd" then use empty. Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358206 Share on other sites More sharing options...
doubledee Posted June 30, 2012 Author Share Posted June 30, 2012 I'd say it depends on what the field is. For text fields like names, urls, emails, etc I generally just let it be an empty string and define the field not null. If the field is storing an ID value which is a reference to another table, or a date/time value, I will set it to NULL when left empty, rather than use a value like 0 or something. Is it correct that an Input Field and text Area that are left blank will always result in an Empty String being assign to the $_POST? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358211 Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2012 Share Posted June 30, 2012 Make a form with all the types of fields, submit it and print_r() the $_POST array. Then you can see for yourself which fields do what when blank. Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358218 Share on other sites More sharing options...
doubledee Posted June 30, 2012 Author Share Posted June 30, 2012 Make a form with all the types of fields, submit it and print_r() the $_POST array. Then you can see for yourself which fields do what when blank. Well, I did that last night but just wanted some reassurance I was understanding things correctly, since I couldn't find any supporting info online. But back to my OP, do you think I should leave Empty Strings as is, or convert them to NULLs? It seems to me it would be better to convert things to NULL's, since that more accurately represents what an unanswered field really is. Then again, a lot of people say that a table with a lot of NULL's in it is a sign of poor database design?! The Form I am currently working on is User Details, so a lot of the fields are "optional"... Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358219 Share on other sites More sharing options...
rythemton Posted June 30, 2012 Share Posted June 30, 2012 But back to my OP, do you think I should leave Empty Strings as is, or convert them to NULLs? It seems to me it would be better to convert things to NULL's, since that more accurately represents what an unanswered field really is. Then again, a lot of people say that a table with a lot of NULL's in it is a sign of poor database design?! The Form I am currently working on is User Details, so a lot of the fields are "optional"... I'm afraid you are asking a very opinionated question here. It is really personal preference. As long as you're consistent on a per field basis, it really doesn't matter, IMHO. If your scripts expect NULL, and are getting NULL from the database, it will work. If your scripts expect Empty Strings, and are getting Empty Strings from the database, it will work. If your scripts expect NULL, and get Empty Strings, or vice versa, you'll have issues. Quote Link to comment https://forums.phpfreaks.com/topic/265055-null-or/#findComment-1358224 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.