MySQL查询 [英] MySQL Query
问题描述
如果我有这样的表设置:
名称| VARCHAR
电子邮件| VARCHAR
年龄| TINYINT | NULL(默认值:NULL)
我希望用户输入他或她的姓名,电子邮件和年龄 - 但AGE
是可选的。 />
我的插入内容如下:
INSERT INTO数据(姓名,电子邮件,年龄)VALUES(''name'','' $ email'',$ age)
这一切都很好,除非用户没有输入年龄。然后$ age是一个空变量。
一个空变量。我认为,因为表格设置在Age
可以为NULL,所以这应该没问题。但MySQL给了我一个错误。
如果$ age是一个数字,那就没问题了。
无论如何要让它接受$ age为空变量(并且只需将它设为
NULL)?我知道我可以说:
if(empty($ age)){$ age = 0; }
并按照这种方式编写,但这仅仅是一个例子,并且有很多
更多的变量可能是空的。
谢谢。
name'',''
email'',
年龄)
这一切都很好,除非用户没有输入年龄。然后
If I have a table set up like this:
Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)
And I want the user to enter his or her name, email, and age - but AGE
is optional.
My insert would look something like:
INSERT INTO data (Name, Email, Age) VALUES (''$name'', ''$email'', $age)
This is all good, except if the user doesn''t enter an age. Then $age is
an empty variable. I thought that since the table is set up where Age
can be NULL, that this should be fine. But MySQL is giving me an error.
If $age is a number, it is no problem.
Anyway to make it accept $age as an empty variable (and just make it
NULL)? I know I could say:
if (empty($age)) { $age = 0; }
and write it that way, but this is just an example and there are many
more variables that could be empty.
Thanks.
name'', ''
email'',
age)
This is all good, except if the user doesn''t enter an age. Then
这篇关于MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!