Jump to content

Recommended Posts

I have a form I am using to add a record to a postgresql database

 

I am getting an error when I try to insert an empty integer field.

 

I don't want to put 0 in the empty fields

 

How do I handle this? Do I need to test each integer field with an if statement?

 

Thanks

 

The insert statement:

 

INSERT into "markets" VALUES (DEFAULT, 'Test Market', 'email1', '', '', 'email2', 20, 10, 16, , , 'string', 35, 100, 25, 29, 30, 34, , , , )

 

"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})";

Link to comment
https://forums.phpfreaks.com/topic/175858-solved-empty-integer-field-in-a-form/
Share on other sites

Well, I don't use postgresql but in MySQL, you can set a field to accept NULL (blank) values.

 

However, if you can't do that, just replace zeros with NULLS for any output and replace NULLS with zeros to add to the database:

if($from_database == 0){
     $from_database = '';
}
if($to_database == '' || $to_database == NULL || !$to_database){
     $to_database = 0;
}

 

Handy PHP

You should be validating form info before putting it in the db in the first place, so putting in something if nothing exists shouldn't be a problem.. nonetheless, the default field value in your db as Handy mentioned would be better.

The fields are set to accept nulls but apparently I need to explicitly pass a NULL value to the database and not just an empty integer like: ,,

 

I can't replace zeros because there are cases where I want the value to be set as zero

 

So I am still rather stuck here

 

Pricelevel2 and 3 for example won't be used very often so I don't want to have to fill every single field in the form out every time

 

Well, I don't use postgresql but in MySQL, you can set a field to accept NULL (blank) values.

 

However, if you can't do that, just replace zeros with NULLS for any output and replace NULLS with zeros to add to the database:

if($from_database == 0){
     $from_database = '';
}
if($to_database == '' || $to_database == NULL || !$to_database){
     $to_database = 0;
}

 

Handy PHP

The fields are set to accept nulls but apparently I need to explicitly pass a NULL value to the database and not just an empty integer like: ,,

 

But is it set up to make it null by default?

 

 

Anyways, dunno how you have your form setup but in general you could just for instance loop through the $_POST array.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.