MySQL and PHP - insert NULL rather than empty string MySQL and PHP - insert NULL rather than empty string php php

MySQL and PHP - insert NULL rather than empty string


To pass a NULL to MySQL, you do just that.

INSERT INTO table (field,field2) VALUES (NULL,3)

So, in your code, check if $intLat, $intLng are empty, if they are, use NULL instead of '$intLat' or '$intLng'.

$intLat = !empty($intLat) ? "'$intLat'" : "NULL";$intLng = !empty($intLng) ? "'$intLng'" : "NULL";$query = "INSERT INTO data (notes, id, filesUploaded, lat, lng, intLat, intLng)          VALUES ('$notes', '$id', TRIM('$imageUploaded'), '$lat', '$long',                   $intLat, $intLng)";


This works just fine for me:

INSERT INTO table VALUES ('', NULLIF('$date',''))

(first '' increments id field)


If you don't pass values, you'll get nulls for defaults.

But you can just pass the word NULL without quotes.