After an hour or so of playing around with things, I think I found a solution. I'd like to hear from the experts if my solution is sound or if it's a miracle that it works at all. My problem was to have the "complete" field, which was set to NULL, remain NULL after a user left a form field blank. Previous to my solution, a blank form field for "complete" caused a string of length = 0 to be inserted into the "complete" field. Here's how I think I fixed it:
There are two places where data is inserted into my database: add new data and update data.
For add new data, I'm using this:
Code: Select all
if ($complete == "")
{
$insertIt=mysql_query("INSERT INTO `$table` (`title`, `date`, `full`) VALUES ('$title', '$date', '$full') ",$db);
}
else
{
$insertIt=mysql_query("INSERT INTO `$table` (`title`, `date`, `full`, `complete`) VALUES ('$title', '$date', '$full', '$complete') ",$db);
}
What I think I see happening with this code is that the "complete" field, when left blank, gets no data inserted. If the field is NULL by default, it remains NULL because nothing was inserted. Is that correct?
For updated data, I'm using this:
Code: Select all
if ($complete == "")
{
$updateIt=mysql_query("UPDATE ".$table." SET title='$title', date='$date', full='$full', complete=NULL WHERE id='$id'",$db);
}
else
{
$updateIt=mysql_query("UPDATE ".$table." SET title='$title', date='$date', full='$full', complete='$complete' WHERE id='$id'",$db);
}
On this code, I think that an update that leaves the "complete" field blank or deletes the text previously in that field will set it to NULL. Is this correct?
Any comments from the experts? Am I in for any headaches if I continue to use this code? Any insights are greatly appreciated.