Your problem is that you have single quotes inside your SQL:
INSERT INTO product (first_field, second_field, third_field, my_date) VALUES ('$data[0]', '$data[1]', '$data[2]', '$data[3]')
therefore, if $data[0] is the string "NULL" , you will get the following:
INSERT INTO product (first_field, second_field, third_field, my_date) VALUES ('NULL', ...
and you try to insert a string containing NULL, not a literal NULL. You will have to do your quotes inside the $data values, and not inside your SQL:
# Warning: my PHP is a bit rusty but I think this is right if(empty($data[0])) { $data[0] = "NULL"; } else { $data[0] = "'" . pg_escape_string(utf8_encode($data[$c])) . "'"; }
And then:
pg_query($_db, "INSERT INTO product (first_field, second_field, third_field, my_date) VALUES ($data[0], $data[1], $data[2], $data[3])";
Or better, switch to PDO and use the prepared instructions.
source share