簡體   English   中英

MySQL不插入NULL值

[英]MySQL not inserting NULL Value

這對您來說很容易,但是我是菜鳥,所以我需要幫助。

這是我的代碼,無論出於何種原因,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");
}

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。

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”中的字段允許為NULL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM