【问题标题】:MySQL not inserting NULL ValueMySQL 不插入 NULL 值
【发布时间】:2013-09-18 13:32:52
【问题描述】:

这对你来说很容易,但我是菜鸟,所以我需要帮助。

这是我的代码,无论出于何种原因 else 语句都没有执行。如果我更改 $variable = NULL 的 NULL 值,它可以工作,但会在我的数据库中输入一个空字符串而不是 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");
}

【问题讨论】:

  • 警告您的代码容易受到 sql 注入攻击。使用参数化查询,这也将解决您的问题。
  • 你的栏目允许NULL吗?
  • 如果您的列默认值为 NULL - 只是不要在 comments 列中插入任何内容,并将其从插入语句中删除
  • 你为什么要从你的实际陈述中省略列列表?
  • 您将 NULL 作为变量。它应该在 SQL 语句中。删除它周围的点和引号。 ". NULL ."NULL

标签: php mysql null


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这应该可以按预期工作。

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

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

    或将默认列值设置为 NULL。

    【讨论】:

      【解决方案3】:

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

      想想你将如何获得 SQL:

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

      数据库可以理解为字符串

      【讨论】:

      • OP 在哪里用撇号括起来?
      【解决方案4】:

      只需将 '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。请确保相关字段“cmets”允许为 NULL。

      【讨论】:

        猜你喜欢
        • 2012-10-28
        • 2019-04-28
        • 1970-01-01
        • 1970-01-01
        • 2019-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多