Author Topic: How do I insert a NULL?  (Read 1734 times)

0 Members and 1 Guest are viewing this topic.

Offline clay1Topic starter

  • Enthusiast
  • Posts: 161
    • View Profile
How do I insert a NULL?
« on: October 10, 2009, 04:43:54 PM »
I thought I had this fixed but the problem has come back.

I have a form which has about a dozen fields some are strings and some are integers

Not all of the fields will be filled out for each record.

My problem is inserting the record when some of the fields are empty

I put this in my script: 

if (trim($value) == '') {
    $_POST[$key] = 'null';
  }

And that works when the integers are empty, but when it's a string I end up with the string 'null' in my record. Then when doing something like:

if (!empty($leadsemailcc)){
or if(isset($leadsemailcc)

It's considering the string 'null' as NOT NULL

If I user $_POST['key'] = NULL; then the insert fails

How the heck does PG handle this stuff?

Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: How do I insert a NULL?
« Reply #1 on: October 10, 2009, 11:32:26 PM »
The important thing here is the interface you're using to PG and how you pass data to it.  And yep the string "null" is not null, it's the string "null".  PHP's null is also not the same as postgres null, though they share some similarities.

Anyway, how do you pass this data to postgres?
Your php questions answered at Flingbits

Offline clay1Topic starter

  • Enthusiast
  • Posts: 161
    • View Profile
Re: How do I insert a NULL?
« Reply #2 on: October 11, 2009, 01:14:32 AM »
What do you mean by how do I pass the data?

I am using php and the following pg_query, but I'm not sure if that is what you are asking about

Code: [Select]
pg_query($conn, "INSERT into \"markets\" VALUES (DEFAULT, '{$market}', '{$leadsemail}', '{$leadsemailcc}', '{$invoiceemail}', '{$invoiceemailcc}', {$fullprice}, {$halfprice}, {$pricelevel1}, {$pricelevel2}, {$pricelevel3}, '{$owner}', {$fullpriceagefrom}, {$fullpriceageto}, {$halfpriceagefrom}, {$halfpriceageto}, {$pricelevel1agefrom}, {$pricelevel1ageto}, {$pricelevel2agefrom}, {$pricelevel2ageto}, {$pricelevel3agefrom}, {$pricelevel3ageto})");

Offline btherl

  • Guru
  • Fanatic
  • *
  • Posts: 3,791
  • Gender: Male
  • Matt is the best!
    • View Profile
Re: How do I insert a NULL?
« Reply #3 on: October 11, 2009, 02:22:43 AM »
Yep that's exactly it.  Here's two examples:

INSERT INTO table (textcolvalues ('null')
INSERT INTO table (textcolvalues (null)

The first query will insert the string 'null', the second will insert a null value.  The difference is the quotes.
Your php questions answered at Flingbits