1

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.

2
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by shammi.khan.73
1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.