I have a MySQL statement that inserts some variables into the database. I recently added 2 fields which are optional ($intLat, $intLng). Right now, if these values are not entered I pass along an empty string as a value. How do I pass an explicit NULL value to MySQL (if empty)?
$query = "INSERT INTO data (notes, id, filesUploaded, lat, lng, intLat, intLng)
VALUES ('$notes', '$id', TRIM('$imageUploaded'), '$lat', '$long',
'$intLat', '$intLng')";
mysql_query($query);
This works just fine for me:
(first
''
increments id field)Check the variables before building the query, if they are empty, change them to the string NULL
To pass a NULL to MySQL, you do just that.
So, in your code, check
if $intLat, $intLng
areempty
, if they are, useNULL
instead of'$intLat'
or'$intLng'
.your query can go as follows:
All you have to do is:
$variable =NULL;
// and pass it in the insert query. This will store the value as NULL in mysql db