在我的PHP代码查询中,当我将空值放入表中时,我遇到了一些错误.列名"number1","number2"和"number3"是整数,可以具有空值.
如果没有空值,则查询有效
查询就像这样 插入表(number1,number2,number3)的值(1,2,3);
但是当我在PHP上的输入表单上留下一个空白值时,
例如,我不会为列"number2"设置值,查询将如下所示.
插入表(number1,number2,number3)值(1,,3);
它说ERROR:语法错误在或附近","SQL状态:42601
有没有人知道如何解决这个问题?
这个查询可能是解决方案,但还有其他方法吗? insert into table(number1,number2,number3)values(1,null,3);
因为我有这么多变量,并且有点懒,放置一些条件,比如当该变量返回空值时,则值="null"
Sim*_*stö 16
您可以NULL通过键入NULL来插入值:
INSERT INTO table(number1,number2,number3) VALUES (1,NULL,3);
Run Code Online (Sandbox Code Playgroud)
如果你有一个变量,并且当该变量为空时你想插入一个NULL值,你可以使用NULLIF单引号括起来的变量为它做准备(这是一个很脏的解决方案,因为你必须将变量视为一个空字符串然后将其转换为整数):
INSERT INTO table(number1,number2,number3) VALUES (1,NULLIF('$var','')::integer,3);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31322 次 |
| 最近记录: |