sql server - How to put Null value in a SQL sever via php code

638

I have the following code, which will get values from these 9 variables and insert them in to a table. However, I don't know how to insertNULL value in to my table. As shown in line 10, if I try to use the keywordNULL, it consider as a string.

        $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));

I will be getting values for these 9 variables via text boxes in a webpage. So, I can't manually enterNULL in my query.

866

Answer

Solution:

Instead of:

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

Use:

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

You don't need to specify a variable for it, just write "NULL" without string marking signs (') into your query.

Greetz

983

Answer

Solution:

Don't use the qoutes "":

$Status = "NULL";

Use:

$Status = null;

835

Answer

Solution:

If you have not default value for Status column another than NULL, simply do not use this column in the columns list of insert statement.

People are also looking for solutions to the problem: php - Magento error in product page: "Integrity constraint violation Duplicate entry for key"

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.