Usually you add regular values ββto mySQL, starting with PHP:
function addValues($val1, $val2) { db_open(); // just some code ot open the DB $query = "INSERT INTO uradmonitor (db_value1, db_value2) VALUES ('$val1', '$val2')"; $result = mysql_query($query); db_close(); // just some code to close the DB }
When your values ββare empty / null ($ val1 == "" or $ val1 == NULL), and you want NULL to be added to SQL, and not 0 or an empty string, to the following:
function addValues($val1, $val2) { db_open(); // just some code ot open the DB $query = "INSERT INTO uradmonitor (db_value1, db_value2) VALUES (". (($val1=='')?"NULL":("'".$val1."'")) . ", ". (($val2=='')?"NULL":("'".$val2."'")) . ")"; $result = mysql_query($query); db_close(); // just some code to close the DB }
Note that null must be added as "NULL", and not as "NULL". Non-empty values ββmust be added as ". $ Val1." '" Etc.
Hope this helps, I just had to use this for some hardware data loggers, some of which collect temperature and radiation, while others only collect radiation. For those who do not have a temperature sensor, I needed NULL, not 0, for obvious reasons (0 is also the accepted temperature value).
radhoo Oct 10 '13 at 14:32 2013-10-10 14:32
source share