我知道之前已经问过这个问题。我找到了帖子,然而,它没有解决我的问题,因为我的列确实允许null。
我想在我的整数列中插入一个NULL值。
if ($startYear == "") {
$startYear = NULL;
}
mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')");
在你提问之前,我已经检查过$startYear
是否为空,它是,所以这应该有效。
当我运行此查询时,它会在数据库行中输入0
。它不会像我想要的那样插入NULL
。
这是我的专栏结构:
http://i.imgur.com/XuWq9Km.png
一个重要的事情是,我无法修改列的结构。这是禁区。
提前致谢。
答案 0 :(得分:0)
问题是你实际上并没有在查询中插入字符串'NULL',而是一个php null
,这使得你的查询插入空字符串,这在数据库中被转换为0。
你需要这样做:
mysqli_query($con, "INSERT INTO Application (startYear) VALUES (NULL)");
答案 1 :(得分:0)
你正在插入一个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