2

This is going to be an easy one for you but I'm a noob so I need help.

Here is my code, for whatever reason the else statement isn't executing. If I change the NULL value for a $variable = NULL it works, but enters an empty string into my database rather than the NULL value. Can anyone help with why?

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");
}
4

4 回答 4

3

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");
                    }
于 2013-09-18T13:36:12.653 回答
0

只需用不带引号的 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。请确保有问题的“评论”字段允许为 NULL。

于 2013-09-18T13:56:42.513 回答
0

这应该按预期工作。

如果这不起作用,请尝试将列设置为 int。

INSERT INTO table (val1, val2) VALUES('String Value', NULL);

或将默认列值设置为 NULL。

于 2013-09-18T13:36:26.317 回答
0

NULL 值不能包含在“撇号”中

想想你将如何获得 SQL:

INSERT INTO table_name VALUES ('null','');?

数据库可以理解为字符串

于 2013-09-18T13:37:09.540 回答