grahamgr3 Posted January 3, 2015 Share Posted January 3, 2015 When entering values into our tables in mysql is it better to enter specifically NULL where no value goes, or is it ok if it is just blank. Because when I submit a form that has no value for one of the fields, I can't write NULL in the insert query because it might have a value. How to get around that? Link to comment Share on other sites More sharing options...
HartleySan Posted January 3, 2015 Share Posted January 3, 2015 NULL and an empty string are two different things. An empty string means that there is a value, but that value is nothing, whereas NULL is the absence of a value. As such, depending on your data and what it's being used for, you need to make the determination for which you think is best in which scenario. This may offer further info: http://programmers.stackexchange.com/questions/32578/sql-empty-string-vs-null-value As for setting NULL in SQL, when you send the parameters to your PHP script, if something is set to an empty string, then you have to set it to NULL. If you're using a regular mysqli_execute function call, then the following is one way of handling it: $param1 = $_POST['param1'] !== '' ? $_POST['param1'] : 'NULL'; Link to comment Share on other sites More sharing options...
Larry Posted January 4, 2015 Share Posted January 4, 2015 It takes extra work, but I always try to provide NULL instead of an empty string. Link to comment Share on other sites More sharing options...
Recommended Posts