0

I try to import a csv file in a table in PostgreSQL 13 and php form (beginner).
There are two foreign keys integer type in the table, one can be empty. So when I import the file I have the common message invalid input syntax for type integer for one of them.
Following this post, NullIf() function seems to be a simple solution.

I can't find the right way to use it in :

$sql = "INSERT INTO activite.us (numus, gidgeo, gidaxe)
        VALUES ('".$getData[0]."','".$getData[10]."', NULLIF('".$getData[11]."', '') " ;

The foreign key is the last variable.

  • I tried to replace second arg with NULL ;
  • I tried 'NULLIF("...")' and some others false combo.

How to fix that ? Thanks.

Leehan
  • 145
  • 7
  • (Possible) side note: Do not use string interpolation or concatenation to get values into SQL queries. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameterized queries. – sticky bit Mar 10 '21 at 11:44
  • This depends on what the syntax error actually is, so please show the entire error message in your question. Pretty sure `NULLIF` is not the answer. A best practices is to create a table that has all `varchar` fields and import into that and then do clean up on/before transfer to final table. – Adrian Klaver Mar 10 '21 at 16:28

1 Answers1

0

(Even if PDO would be better,) an answer is

NULLIF('".$getData[11]."', '')::integer)
Leehan
  • 145
  • 7