I would like to insert value NULL
in to sql server table.
我想在sql server表中插入值NULL。
$value = $_POST['value'];
then PDO with (INSERT .... :value.....)
and execute(array(':value' => "$value"));
but when html input field is empty I got empty field on sql but I would like to see NULL
I tried with
和执行(数组(“价值”= >“美元价值”));但是当html输入字段为空时,我在sql中得到了空字段,但我希望看到我尝试过的空字段
if ($_POST['value'] ==''){$value = NULL;}else{....}
but nothing happens.
但什么都没发生。
My sql table looks like:
我的sql表如下:
[field] [varchar](20) NULL,
1 个解决方案
#1
4
Regarding NULL
values in prepared statements in general: ANY NULL
value that was actually bound into statement, is always being sent to database as NULL
. No exceptions.
关于准备语句中的空值:实际上被绑定到语句中的任何空值,总是作为空值发送到数据库。没有例外。
Example:
例子:
$stmt = $db->prepare("SELECT isnull(?)");
$stmt->execute(array(NULL));
var_dump($stmt->fetchColumn());
// string(1) "1"
Regarding your particular code, as it was pointed out in the comments, your problem is caused by wrong syntax. In PHP, variables have to be addressed without quotes. Quotes are used in PHP to delimit strings. While variables, again, have to be addressed without quotes.
关于您的特定代码,正如注释中指出的,您的问题是由错误的语法导致的。在PHP中,变量必须在没有引号的情况下进行寻址。PHP中使用引号分隔字符串。而变量,同样需要在没有引号的情况下进行处理。
#1
4
Regarding NULL
values in prepared statements in general: ANY NULL
value that was actually bound into statement, is always being sent to database as NULL
. No exceptions.
关于准备语句中的空值:实际上被绑定到语句中的任何空值,总是作为空值发送到数据库。没有例外。
Example:
例子:
$stmt = $db->prepare("SELECT isnull(?)");
$stmt->execute(array(NULL));
var_dump($stmt->fetchColumn());
// string(1) "1"
Regarding your particular code, as it was pointed out in the comments, your problem is caused by wrong syntax. In PHP, variables have to be addressed without quotes. Quotes are used in PHP to delimit strings. While variables, again, have to be addressed without quotes.
关于您的特定代码,正如注释中指出的,您的问题是由错误的语法导致的。在PHP中,变量必须在没有引号的情况下进行寻址。PHP中使用引号分隔字符串。而变量,同样需要在没有引号的情况下进行处理。