简体   繁体   English

PHP值null插入sql server插入语句

[英]PHP value null into sql server insert statement

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")); 然后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上得到了空字段,但是我想看到NULL我尝试了

if ($_POST['value'] ==''){$value = NULL;}else{....}

but nothing happens. 但什么也没发生。

My sql table looks like: 我的SQL表看起来像:

[field] [varchar](20)  NULL,

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 . 关于NULL在一般准备语句值:任何NULL真的绑定到语句值, 总是被发送到数据库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. 在PHP中,变量必须不加引号。 Quotes are used in PHP to delimit strings. 在PHP中,引号用于分隔字符串。 While variables, again, have to be addressed without quotes. 同样,变量必须无引号地处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM