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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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."' Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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'])); Quote Link to comment 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 )";} Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.