sql server - How to put Null value in a SQL sever via php code
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.
Answer
Solution:
Instead of:
Use:
You don't need to specify a variable for it, just write "NULL" without string marking signs (') into your query.
Greetz
Answer
Solution:
Don't use the qoutes "":
Use:
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.