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
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

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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 )";}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.