Php MySQL在INT列中插入空值

Php MySQL在INT列中插入空值,php,mysql,Php,Mysql,我知道以前有人问过这个问题。我发现了这篇文章,但是,它并没有解决我的问题,因为我的列允许null 我想在整数列中插入一个空值 if ($startYear == "") { $startYear = NULL; } mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')"); 在您询问之前,我已经检查过$startYear是否为空,它是空的,所以这应该可以工作 当我运行这个查询

我知道以前有人问过这个问题。我发现了这篇文章,但是,它并没有解决我的问题,因为我的列允许null

我想在整数列中插入一个空值

if ($startYear == "") {
    $startYear = NULL;
}

mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')");
在您询问之前,我已经检查过$startYear是否为空,它是空的,所以这应该可以工作

当我运行这个查询时,它会在数据库行中输入一个0。它不像我想要的那样插入NULL

以下是我的专栏结构:

重要的一点是,我不能修改柱的结构。禁止入内


提前感谢。

问题是您实际上并没有在查询中插入字符串“NULL”,而是一个php NULL,它使您的查询插入空字符串,这在数据库中被转换为0

您需要这样做:

  mysqli_query($con, "INSERT INTO Application (startYear) VALUES (NULL)");

您正在插入一个PHP空值,当在字符串上下文中使用它时,它将变成一个空字符串—这是最初的设置。您必须插入一个带有字母n、u、l、l`的文字字符串,才能使其正常工作:

if ($startYear == '') {
   $startYear = 'NULL'; // string null
}
$sql = "INSERT ..... VALUES ($startYear)";
那会产生什么

INSERT ... VALUES (NULL)
要扩展:

   $foo = null; // actual PHP null value
   $bar = "$foo"; // insert this php null into a string
   echo strlen($bar); // returns 0

   $foo = 'null' // string with 4 letters: n,u,l,l
   $bar = "$foo";
   echo strlen($bar); // returns 4

@Nic是否正确回答了您的问题?如果是,请将其标记为正确。如果没有,还有更好的答案吗?