I have a form where users enter data. If no data is entered in a given field my code still inserts a 0 into the MySQL database even though I allow Null values.

This is a problem for two reasons. In some of my fields I'm using TINY INTEGER as a boolean but I actually need three values. I need True, False, or Unknown. Unknown could be represented by Null but since I don't know how to put Nulls into the database I've resorted to using 0=False, 1=True, and 2=Unknown. Is there a better way of doing this? My code for this looks like this:

$restroomsyn = ($_POST["restroomsyn"]);

In another field I will be storing another INTEGER value where 0 is a possible answer. I also need to be able to store UNKNOWN in this field as well which could be represented by a NULL if I knew how to handle it, both going in and coming out. My code for this looks like this:

$totalwindows = ($_POST["totalwindows"]);

My background is in MS Access and ASP/VBScript so I'm just trying to learn this PHP/MySQL stuff.

What's the simplest way to test a $_POST or $_GET variable and if it's empty then make the variable NULL so that my insert statement inserts a NULL value?

7 Years
Discussion Span
Last Post by darkagn

You could try:

  $restroomsyn = ($_POST["restroomsyn"]);
  $restroomsyn = null;

Then when you are inserting use is_null to check for null and set the column to NULL in this situation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.