如何通过PHP代码将Null值放在SQL服务器中

时间:2014-07-09 14:18:03

标签: php sql-server

我有以下代码,它将从这9个变量中获取值并将它们插入到表中。但是,我不知道如何将NULL值插入到我的表中。如第10行所示,如果我尝试使用关键字NULL,则将其视为字符串。

        $WorkOrder = $_POST['WorkOrder'];
        $Originated = $_POST['Originated'];
        $CustID = $_POST['CustID'];
        $Customer = $_POST['Customer'];
        $Contact = $_POST['Contact'];
        $Completed = $_POST['Completed'];
        $AccountNum = $_POST['AccountNum'];
        $Description = $_POST['Description'];
        $Status = $_POST['Status'];
        $Status = "NULL"; //Testing with NULL

        $insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer, Contact, Completed, AccountNum, Description, Status) 
                       VALUES ('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description','$Status')";
        $data = sqlsrv_query($connectString, $insertquery) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));

我将通过网页中的文本框获取这9个变量的值。因此,我无法在查询中手动输入NULL

3 个答案:

答案 0 :(得分:0)

而不是:

$insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer,  Contact, Completed, AccountNum, Description, Status) VALUES ('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description','$Status')";

使用:

$insertquery = "INSERT INTO ElectronicShop(WorkOrder, Originated, CustID, Customer, Contact, Completed, AccountNum, Description, Status) VALUES
('$WorkOrder','$Originated','$CustID','$Customer','$Contact','$Completed','$AccountNum','$Description', NULL)";

您不需要为它指定变量,只需写入" NULL"在您的查询中没有字符串标记符号(')。

格尔茨

答案 1 :(得分:0)

不要使用qoutes"":

  

$ Status =" NULL&#34 ;;

使用:

  

$ Status = null;

答案 2 :(得分:0)

如果Status列的默认值不是NULL,则不要在insert语句的列列表中使用此列。