Php MySQL没有插入空值
这对你来说很容易,但我是个笨蛋,所以我需要帮助 这是我的代码,不管出于什么原因else语句没有执行。如果我更改$variable=NULL的NULL值,它会工作,但会在数据库中输入一个空字符串,而不是NULL值。有人能帮忙解释一下原因吗Php MySQL没有插入空值,php,mysql,null,Php,Mysql,Null,这对你来说很容易,但我是个笨蛋,所以我需要帮助 这是我的代码,不管出于什么原因else语句没有执行。如果我更改$variable=NULL的NULL值,它会工作,但会在数据库中输入一个空字符串,而不是NULL值。有人能帮忙解释一下原因吗 if (isset($_POST['agreeto'])) { $enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score,
if (isset($_POST['agreeto']))
{
$enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
" VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(), '" . $friend . "', '" . $return . "', '" . $service . "', '" . $_POST['comments'] . "')";
}else
{
$enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
" VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(), '" . $friend . "', '" . $return . "', '" . $service . "', " . NULL . ")" or die("couldn't execute my query");
}
MySQL需要string=NULL,并将php NULL常量的值传递给它
if (isset($_POST['agreeto']))
{
$enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
" VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(), '" . $friend . "', '" . $return . "', '" . $service . "', '" . $_POST['comments'] . "')";
}else
{
$enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
" VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(), '" . $friend . "', '" . $return . "', '" . $service . "',NULL)" or die("couldn't execute my query");
}
这应该像预期的那样起作用 如果不起作用,请尝试将列设置为int
INSERT INTO table (val1, val2) VALUES('String Value', NULL);
或将默认列值设置为NULL。空值不能包含在“撇号”中 考虑如何获得SQL:
INSERT INTO table_name VALUES ('null','');?
数据库可能理解为字符串,只需将“NULL”值替换为不带引号的NULL
$enter_feedback = "INSERT INTO feedback" . //(initial, surname, country_id, date, friend_score, return_score, service_score, comments)
" VALUES('" . $initial. "', '" . $lastname . "', '" . $country_id . "', NOW(), '" . $friend . "', '" . $return . "', '" . $service . "', NULL) " or die("couldn't execute my query");
完成此操作并保存记录后,通过PHPMYADMIN浏览时,该字段的数据库列中将显示NULL。请确保有问题的字段“comments”允许空值。警告您的代码容易受到sql注入攻击。使用参数化查询,这也会解决您的问题。您的列允许NULL吗?如果您的列默认值为NULL-只需不在
comments
列中插入任何内容,并将其从insert语句中删除。为什么从实际语句中忽略列列表?您将NULL作为变量。它应该在SQL语句中。删除它周围的点和引号“.NULL.”
到刚才的NULL
用撇号括起来的OP在哪里?