Jump to content

function that makes a lot of vars empty


coupe-r

Recommended Posts

Hey guys,

 

OK, heres what I would like to do.  On my form, I have 50+ variables and not all of them require a response.  If inserted, they insert a blank in the DB, instead of NULL.  First, does this even matter?

 

If it does, how can I create a function that checks the var to see if its empty, and if empty $var = NULL;

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/
Share on other sites

<?php
function checkVars($var){
  if(empty($var)){
      $var = NULL;
      return $var;
  }else{
      return $var;
  }
}
?>

 

EDIT:

by the way if u r trying to put null variables directly to db with query it will add zero chars to the query and will leave u with blank or syntax error.. depends if u ll quote it or not

u see that function accepts only 1 input so u 'd have to do it through the loop somehow or 1 by 1 =)

 

for sql insert:

SQL is a string based language. SQL's NULL must be represented by NULL with no quotes

so query should be like this:

INSERT INTO foo SET bar = NULL;

however if you try to insert the PHP NULL directly into the query, it will add zero characters to the query

 

 

 

 

yeah i know that. as i said before php null is not equal to sql null therefore you cannot insert it via variable.

you can try to build your query by checking if var is empty .... smthing like this:

 

$workphone = trim(mysql_real_escape_string($_POST['workphone_txt']));

$query = "INSERT INTO table (field1,field2,field3) VALUES('field1','field2', ";
if(!empty($workphone)){ $query .= "'$workphone')" }
else{ $query .= " NULL )";}

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.