Thanks Matija for your help. My table has column names same as field names in the form. So I think 1st part of the code will work for me. However I am not sure about one thing -
what happens if a field in the form is left blank? Will $val contain null for that field?