Jump to content


Photo

Pass back NULL to SQL


  • Please log in to reply
3 replies to this topic

#1 jamina1

jamina1
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 26 April 2006 - 11:43 PM

Hi! I'm making an HTML backend for a site I'm working on. Basically, I'm trying to make a more user friendly interface for people interacting with our database.

Currently I have a form that validates itself before submitting. Problem is that some of these fields will be left blank from time to time. If they ARE left blank, I need PHP to pass NULL to the SQL table so that it doesn't display that value. Currently it's passing ''.

I have a form getting values from the Database, which can then be edited and re-submitted. If a value is set to NULL in the database, it reads out as " " in the form value.

Before I submit it back to the database, I check to make sure it doesn't equal "" or isn't empty() or even isn't Null. I even SET it to Null if it is one of these things. Still, I don't get the actual Null value in the SQL database.

if(trim($_POST['SubCat'.$value])==""||trim($_POST['SubCat'.$value])===NULL||empty($_POST['SubCat'.$value])) {
            $_SESSION['SubCat'.$value] = NULL;
            } else {
            $_SESSION['SubCat'.$value]=$_POST['SubCat'.$value];
            }
That checks to see if its empty, but still SQL doesn't see NULL. The $value variable is there because the Post names are dynamically generated depending on which DB entries they are editing.

HELP!


#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 27 April 2006 - 03:57 PM

can you just use a pair of empty quotes instead of NULL:

$_SESSION['SubCat'.$value] = "";

now you have a declared var with no value. same effect, better result.
"Confidence in the face of risk."

#3 jamina1

jamina1
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 27 April 2006 - 05:15 PM

[!--quoteo(post=369268:date=Apr 27 2006, 11:57 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Apr 27 2006, 11:57 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
can you just use a pair of empty quotes instead of NULL:

$_SESSION['SubCat'.$value] = "";

now you have a declared var with no value. same effect, better result.
[/quote]

Doesn't work, it just passes an empty variable to SQL, instead of NULL. I absolutely NEED it to read NULL.
The reason for this is the database is used for a navigation menu. If there is just an empty variable, it reads " " as a heading for a navigation bar. Unless it is NULL it will read out as a blank category, which messes up the nav bar.

So, how do I make it tell it NULL?

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 27 April 2006 - 06:37 PM

Use this
if(trim($_POST['SubCat'.$value])==""||trim($_POST['SubCat'.$value])===NULL||empty($_POST['SubCat'.$value])) {
            $_SESSION['SubCat'.$value] = 'NULL';
            } else {
            $_SESSION['SubCat'.$value]=$_POST['SubCat'.$value];
            }

To insert into table you use the word NULL with no quotes

$sql = "INSERT INTO tablename (field1, field2) values (NULL, "value")";

If you set NULL to a variable make sure there are no quotes around it
$nullvalue = 'NULL';
$sql = "INSERT INTO tablename (field1, field2) values ($nullvalue, "value")";

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users