bryan
bryan

Reputation: 9399

Update INT/DECIMAL field with a null/no value

I have a lot of input box's. Each input box is linked with a INT or DECIMAL MySQL field and is displayed inside the text box.

The default value of each INT/DECIMAL is a null so that when a user first opens the page, nothing is shown inside the text box's.

I have an update query that updates the value of each input box to the respected MySQL Field.

My problem is, for each input box that doesn't get anything typed in, the value of the field switches from a NULL to a 0.

I am having trouble figuring out a way to update the un-touched input's to a NULL and not have all my untouched values go to 0. Can anyone help?


Defining my variables basically goes like:

if(nullBlank($_POST['variable1']) == false)
    $variable1 = $_POST['variable1'];
else
    $variable = "";

I've also tried: $variable = null;


My update query basically looks like this:

mysql_query("UPDATE `table` SET `variable1`= '" . $variable1 . "' WHERE something = something

My nullBlank() function

function nullBlank($data)
{
  if(is_null($data))
       $value = true;
  elseif(strlen($data) <= 0)
       $value = true;
  elseif($data === "")
       $value = true;
  else
       $value = false;

 return $value;
}

Upvotes: 0

Views: 2496

Answers (2)

duindain
duindain

Reputation: 545

Perhaps change your code for null checks to

if (is_null($myVariable)) {
    $sql .= "$columnName= NULL";
} else {
    $sql .= "$columnName= '" . mysql_real_escape_string($myVariable) . "'";
}

then call this for each value and it will either null it or quote it

Upvotes: 1

Johannes H.
Johannes H.

Reputation: 6167

Set $variable1 to "NULL" (the string). This way it will end up in the query as NULL, which is what represents NULL in SQL.

Upvotes: 3

Related Questions