Jump to content

Pass back NULL to SQL


jamina1

Recommended Posts

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.

[code]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];
            }[/code]
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!
Link to comment
https://forums.phpfreaks.com/topic/8513-pass-back-null-to-sql/
Share on other sites

[!--quoteo(post=369268:date=Apr 27 2006, 11:57 AM:name=micah1701)--][div class=\'quotetop\']QUOTE(micah1701 @ Apr 27 2006, 11:57 AM) [snapback]369268[/snapback][/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?
Link to comment
https://forums.phpfreaks.com/topic/8513-pass-back-null-to-sql/#findComment-31380
Share on other sites

Use this
[code]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];
            }[/code]

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

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

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

Ray
Link to comment
https://forums.phpfreaks.com/topic/8513-pass-back-null-to-sql/#findComment-31404
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.