Establecer valor en NULL en MySQL


Quiero que un valor se establezca en NULL si no se pone nada en el cuadro de texto en el formulario que estoy enviando. ¿Cómo puedo hacer que esto suceda? He intentado insertar 'NULL' pero esto solo agrega la palabra NULL en el campo.

No estoy seguro de qué código debería proporcionar para esto, solo estoy escribiendo una consulta de ACTUALIZACIÓN.

 98
Author: Backlin, 2012-02-16

9 answers

No pongas NULL entre comillas en tu declaración de actualización. Esto debería funcionar:

UPDATE table SET field = NULL WHERE something = something
 203
Author: Fosco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-13 04:49:25

Probablemente estás citando 'NULL'. NULL es una palabra reservada en MySQL, y se puede insertar / actualizar sin comillas:

INSERT INTO user (name, something_optional) VALUES ("Joe", NULL);
UPDATE user SET something_optional = NULL;
 16
Author: Andrew Ensley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:49:39
UPDATE MyTable
SET MyField = NULL
WHERE MyField = ''
 10
Author: Bassam Mehanni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:49:38
if (($_POST['nullfield'] == 'NULL') || ($_POST['nullfield'] == '')) {
   $val = 'NULL';
} else {
   $val = "'" . mysql_real_escape_string($_POST['nullfield']) . "'";
}

$sql = "INSERT INTO .... VALUES ($val)";

Si pones 'NULL' en tu consulta, entonces solo estás insertando una cadena de 4 caracteres. Sin las comillas, NULL es el valor null real.

 6
Author: Marc B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:49:44

Debe insertar null, no la cadena de 'NULL'.

 5
Author: xdazz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:49:59

Use NULL (sin las comillas a su alrededor).

UPDATE users SET password = NULL where ID = 4
 4
Author: Rocket Hazmat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:49:38

Asumiendo que la columna permite un valor nulo,

$mycolupdate = null; // no quotes

Debería hacer el truco

 3
Author: Mr Griever,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-16 15:51:26

Las respuestas dadas aquí son buenas, pero todavía estaba luchando para publicar NULL y no cero en la tabla mysql.

Finalmente noté que el problema estaba en la consulta de inserción que estaba usando

   $quantity= "NULL";
   $itemname = "TEST";

Hasta ahora todo bien.

Mi consulta de inserción fue mala.

   mysql_query("INSERT INTO products(quantity,itemname) 
   VALUES ('$quantity','$itemname')");

Corregí la consulta para leer.

   mysql_query("INSERT INTO products(quantity,itemname) 
   VALUES ('".$quantity."','$itemname')");

Así que la cantidad is está fuera de la cadena principal. Mi tabla sql ahora aceptada para registrar cantidad nula en lugar de 0

 2
Author: user3629945,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-02 10:37:53

El problema que tuvo es muy probable porque mysql diferencia entre null escrito en mayúsculas y null escrito en minúsculas.

Así que si utilizas una instrucción update con null, no funcionará. Si lo establece en NULL, funcionaría bien.

Gracias.

 2
Author: user7509683,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-03 06:19:31