I want to insert a NULL value into my integer column.

    if ($startYear == "") {


    $startYear = NULL;
}

mysqli_query($con, "INSERT INTO Application (startYear) VALUES ('".$startYear."')");
Before you ask, I have checked if $startYear is ever empty, and it is, so this should work.

When I run this query it inputs a 0 into the database row. It does not insert NULL like I want.

Here is my column's structure:

http://i.imgur.com/XuWq9Km.png

One important thing is that, I can not modify the column's structure. It is off limits.

Thanks in advance.

Well assuming everything else is in order, try this in your code:

$startYear = 1997; # Define variable here, I've used '1997' as an example, note that there's no commas to make it a text value.
if ($startYear == "") {
    $startYear = NULL;
}
mysqli_query($con, "INSERT INTO Application (startYear) VALUES (".$startYear.")");

I think it might be giving 0, because the value entered is not an integer, so hopefully the above code will help solve that. When inserting integers, there is no need to use quotes, as all this does is makes it a string, which of course will not be accepted.

Thank you mattster. It's working..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.