twilitegxa Posted March 28, 2010 Share Posted March 28, 2010 Is there a way to insert 'Null' into a field if a form's posted values has been left empty? I want to allow a user to have the option of filling to a form field or not, but I would like to insert Null instead of a blank space into my form. Is this possible? I have a page that displays the optional field values, and if they are null, I have it set to not show. Quote Link to comment Share on other sites More sharing options...
xcandiottix Posted March 28, 2010 Share Posted March 28, 2010 on the php page before you INSERT the form data into the db you would want something like: $field1=$_POST['formfield1']; if($field1 == ""){ $field1 = NULL; } Then when you have your insert: INSERT (DBfield1) VALUES ($field1) it should work for you. I'm not sure if you need to actually have $field1 equal to NULL or "NULL" since NULL is a php var. You may want to play with it. Hope that helps -K.candiotti Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted March 28, 2010 Author Share Posted March 28, 2010 I tried both with NULL and "NULL" and NULL leaves it blank, but does not set it to NULL and "NULL" makes it say NULL but doesn't set it to NULL. It just makes the value the word NULL. Any other suggestions? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted March 28, 2010 Author Share Posted March 28, 2010 I think I can fix it by adding another if statement: if ($variable != ""){ echo $variable; } Thanks for the help though. This appears to work. Quote Link to comment 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.