在 MySQL 中将值设置为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9314353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set value to NULL in MySQL
提问by muttley91
I want a value to be set to NULL
if nothing is put into the text box in the form I'm submitting. How can I make this happen?
I've tried inserting 'NULL'
but this just adds the word NULL
into the field.
NULL
如果我提交的表单中的文本框中没有放入任何内容,我希望设置一个值。我怎样才能做到这一点?我试过插入,'NULL'
但这只是将单词添加NULL
到字段中。
I'm not sure what code I should provide for this, I'm just writing an UPDATE query.
我不确定我应该为此提供什么代码,我只是在写一个 UPDATE 查询。
回答by Fosco
Don't put NULL
inside quotes in your update statement. This should work:
不要NULL
在你的更新语句中加上引号。这应该有效:
UPDATE table SET field = NULL WHERE something = something
回答by Andrew Ensley
You're probably quoting 'NULL'. NULL is a reserved word in MySQL, and can be inserted/updated without quotes:
您可能会引用“NULL”。NULL 是 MySQL 中的保留字,可以不加引号插入/更新:
INSERT INTO user (name, something_optional) VALUES ("Joe", NULL);
UPDATE user SET something_optional = NULL;
回答by Bassam Mehanni
UPDATE MyTable
SET MyField = NULL
WHERE MyField = ''
回答by Marc B
if (($_POST['nullfield'] == 'NULL') || ($_POST['nullfield'] == '')) {
$val = 'NULL';
} else {
$val = "'" . mysql_real_escape_string($_POST['nullfield']) . "'";
}
$sql = "INSERT INTO .... VALUES ($val)";
if you put 'NULL'
into your query, then you're just inserting a 4-character string. Without the quotes, NULL
is the actual null value.
如果您'NULL'
输入查询,那么您只是插入了一个 4 个字符的字符串。没有引号,NULL
是实际的空值。
回答by xdazz
You should insert null
, not the string of 'NULL'
.
您应该插入null
,而不是'NULL'
.
回答by Rocket Hazmat
Use NULL
(without the quotes around it).
使用NULL
(不带引号)。
UPDATE users SET password = NULL where ID = 4
回答by Mr Griever
Assuming the column allows a null setting,
假设该列允许空设置,
$mycolupdate = null; // no quotes
should do the trick
应该做的伎俩
回答by webs
The answers given here are good but i was still struggling to post NULL and not zero in mysql table.
这里给出的答案很好,但我仍在努力在 mysql 表中发布 NULL 而不是零。
Finally i noted the problem was in the insert query that i was using
最后我注意到问题出在我使用的插入查询中
$quantity= "NULL";
$itemname = "TEST";
So far so good.
到现在为止还挺好。
My insert query was bad.
我的插入查询很糟糕。
mysql_query("INSERT INTO products(quantity,itemname)
VALUES ('$quantity','$itemname')");
I corrected query to read.
我更正了要阅读的查询。
mysql_query("INSERT INTO products(quantity,itemname)
VALUES ('".$quantity."','$itemname')");
So the $quantity is outside of the main string. My sql table now accepted to record null quantity instead of 0
所以 $quantity 在主字符串之外。我的 sql 表现在接受记录空数量而不是 0
回答by user7509683
The problem you had is most likely because mysql differentiates between null written in capital letters and null written in small case.
您遇到的问题很可能是因为 mysql 区分了用大写字母写的 null 和用小写字母写的 null。
So if you used an update statement with null, it would not work. If you set it to NULL, it would work fine.
因此,如果您使用带有 null 的更新语句,它将不起作用。如果您将其设置为 NULL,它将正常工作。
Thanks.
谢谢。