Tengo una declaración de MySQL que inserta algunas variables en la base de datos. Recientemente agregué 2 campos que son opcionales ($ intLat, $ intLng). En este momento, si no se ingresan estos valores, paso una cadena vacía como valor. ¿Cómo paso un valor NULL explícito a MySQL (si está vacío)?
$query = "INSERT INTO data (notes, id, filesUploaded, lat, lng, intLat, intLng)
VALUES ('$notes', '$id', TRIM('$imageUploaded'), '$lat', '$long',
'$intLat', '$intLng')";
mysql_query($query);
php
mysql
null
sql-insert
user547794
fuente
fuente
$query
string,$intLat
and$intLng
are not quoted. So, when the variables are interpolated, it will be, NULL, NULL);
.If you don't pass values, you'll get nulls for defaults.
But you can just pass the word NULL without quotes.
fuente
This works just fine for me:
INSERT INTO table VALUES ('', NULLIF('$date',''))
(first
''
increments id field)fuente
NULLIF(expr1, expr2)
. See dev.mysql.com/doc/refman/5.7/en/…All you have to do is:
$variable =NULL;
// and pass it in the insert query. This will store the value as NULL in mysql dbfuente
Normally, you add regular values to mySQL, from PHP like this:
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 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'" . The non-null values must be added as "'".$val1."'", etc.
Hope this helps, I just had to use this for some hardware data loggers, some of them collecting temperature and radiation, others only radiation. For those without the temperature sensor I needed NULL and not 0, for obvious reasons ( 0 is an accepted temperature value also).
fuente
your query can go as follows:
$query = "INSERT INTO data (notes, id, filesUploaded, lat, lng, intLat, intLng) VALUES ('$notes', '$id', TRIM('$imageUploaded'), '$lat', '$lng', '" . ($lat == '')?NULL:$lat . "', '" . ($long == '')?NULL:$long . "')"; mysql_query($query);
fuente
NULL
. Also, you are using PHPsNULL
(unquoted) keyword, which evaluates to an empty string in a string context, so again, you are assigning an empty string.Check the variables before building the query, if they are empty, change them to the string NULL
fuente
you can do it for example with
UPDATE `table` SET `date`='', `newdate`=NULL WHERE id='$id'
fuente
$query = "INSERT INTO uradmonitor (db_value1, db_value2) VALUES (". (($val1=='')?"NULL":("'".$val1."'")) . ", ". (($val2=='')?"NULL":("'".$val2."'")) . ")";
$query = "INSERT INTO uradmonitor (db_value1, db_value2) VALUES (". (($val1=='')? :("'".$val1."'")) . ", ". (($val2=='')? :("'".$val2."'")) . ")";
fuente