coupe-r Posted September 16, 2010 Share Posted September 16, 2010 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 More sharing options...
kristijan.jurkovic Posted September 17, 2010 Share Posted September 17, 2010 <?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 https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111892 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Thank you very much for that, but how do I now pass in every var? Do I just do this? checkVars($var1, $var2, $var3, etc.....) PS - Thanks for using your first post on me Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111893 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 I insert the vars like this '".$var."' Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111894 Share on other sites More sharing options...
kristijan.jurkovic Posted September 17, 2010 Share Posted September 17, 2010 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 https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111899 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 OK, no problem. So it is OK if the DB fields dont say NULL and are just empty? Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111901 Share on other sites More sharing options...
kristijan.jurkovic Posted September 17, 2010 Share Posted September 17, 2010 well NULL is a marker which specifies that data does not exists in the field. alternatively u can put default value of the field in db as NULL so if u dont input anything in it it will be NULL Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111902 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 The default value is automatically NULL, but it still reads a blank field, not NULL. Here is the var: $workphone = trim(mysql_real_escape_string($_POST['workphone_txt'])); Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111904 Share on other sites More sharing options...
kristijan.jurkovic Posted September 17, 2010 Share Posted September 17, 2010 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 https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111907 Share on other sites More sharing options...
coupe-r Posted September 17, 2010 Author Share Posted September 17, 2010 Perfect, thank you for your time. Link to comment https://forums.phpfreaks.com/topic/213619-function-that-makes-a-lot-of-vars-empty/#findComment-1111908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.