pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Inserting Null Value or String Value
8+ messages / 6 participants
[nested] [flat]

* Inserting Null Value or String Value
@ 2002-10-01 16:54  test test <dcmkx10@yahoo.com>
  0 siblings, 2 replies; 8+ messages in thread

From: test test @ 2002-10-01 16:54 UTC (permalink / raw)
  To: pgsql-php


Hi,

I am trying to insert a date into a date field, but also sometimes I need to insert a null value.  Inserting the null value seems to require not using quotes around null, but if I try to enter a date without quotes around it, it thinks it is a number.

Here is the error I get: Warning: PostgreSQL query failed: ERROR: column "spec_start_date" is of type 'date' but expression is of type 'integer' You will need to rewrite or cast the expression 

The value I am sending is: spec_start_date = 10/01/02

If I surround the date with single quotes it should be fine, but I can't do that when I want to insert a null value.  I know that I can do an "IF <use quotes> ELSE <don't use quotes>" in the SQL string, by I am entering many values and this would really get messy. I was hoping that there was a better solution. Thanks for any help.

Sean



---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 17:37  Keary Suska <hierophant@pcisys.net>
  parent: test test <dcmkx10@yahoo.com>
  1 sibling, 2 replies; 8+ messages in thread

From: Keary Suska @ 2002-10-01 17:37 UTC (permalink / raw)
  To: pgsql-php

on 10/1/02 10:54 AM, dcmkx10@yahoo.com purportedly said:

I am trying to insert a date into a date field, but also sometimes I need to
insert a null value.  Inserting the null value seems to require not using
quotes around null, but if I try to enter a date without quotes around it,
it thinks it is a number.

Here is the error I get: Warning: PostgreSQL query failed: ERROR: column
"spec_start_date" is of type 'date' but expression is of type 'integer' You
will need to rewrite or cast the expression

The value I am sending is: spec_start_date = 10/01/02

If I surround the date with single quotes it should be fine, but I can't do
that when I want to insert a null value.  I know that I can do an "IF <use
quotes> ELSE <don't use quotes>" in the SQL string, by I am entering many
values and this would really get messy. I was hoping that there was a better
solution. Thanks for any help.

If there is a way, I haven't found it yet. But it doesn't have to be
messy--simply create a function that does the if-else for you, and your code
will stay clean.

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 18:00  Roland Roberts <roland@astrofoto.org>
  parent: Keary Suska <hierophant@pcisys.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Roland Roberts @ 2002-10-01 18:00 UTC (permalink / raw)
  To: pgsql-php

>>>>> "Keary" == Keary Suska <hierophant@pcisys.net> writes:

    Keary> If I surround the date with single quotes it should be
    Keary> fine, but I can't do that when I want to insert a null
    Keary> value.  I know that I can do an "IF <use quotes> ELSE
    Keary> <don't use quotes>" in the SQL string, by I am entering
    Keary> many values and this would really get messy. I was hoping
    Keary> that there was a better solution. Thanks for any help.

Why is this too difficult?  You *are* doing some sort of input
validation before you send the data to posgres aren't you?

roland
-- 
		       PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                             RL Enterprises
roland@rlenter.com                     76-15 113th Street, Apt 3B
roland@astrofoto.org                       Forest Hills, NY 11375



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 18:08  Eckhard Hoeffner <e-hoeffner@fifoost.org>
  parent: Keary Suska <hierophant@pcisys.net>
  1 sibling, 1 reply; 8+ messages in thread

From: Eckhard Hoeffner @ 2002-10-01 18:08 UTC (permalink / raw)
  To: pgsql-php

* Keary Suska <hierophant@pcisys.net> [01 10 02 19:37]:

>on 10/1/02 10:54 AM, dcmkx10@yahoo.com purportedly said:
>
>I am trying to insert a date into a date field, but also sometimes I need to
>insert a null value.  Inserting the null value seems to require not using
>quotes around null, but if I try to enter a date without quotes around it,
>it thinks it is a number.
>
>Here is the error I get: Warning: PostgreSQL query failed: ERROR: column
>"spec_start_date" is of type 'date' but expression is of type 'integer' You
>will need to rewrite or cast the expression
>
>If I surround the date with single quotes it should be fine, but I can't do
>that when I want to insert a null value.  I know that I can do an "IF <use
>quotes> ELSE <don't use quotes>" in the SQL string, by I am entering many
>values and this would really get messy. I was hoping that there was a better
>solution. Thanks for any help.
>
>If there is a way, I haven't found it yet. But it doesn't have to be
>messy--simply create a function that does the if-else for you, and your code
>will stay clean.

Maybe you should do the following. At the point where the switch 
"date" or "NULL" in the php-file is: 

if(empty($form_date)){
  $date_var = NULL;
}else $date_var = "'$date'";

$query = "INSERT INTO table (table_row1, date) 
          VALUES ('$value1', $date_var);";

You still have the if/else, but I think there must be somewhere the
switch NULL <=> date          

-- 
Eckhard Höffner
e-hoeffner@fifoost.org
D-80331 München
Tel. +49-89-21 03 18 88 



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 18:41  Papp Gyozo <pgerzson@freestart.hu>
  parent: test test <dcmkx10@yahoo.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Papp Gyozo @ 2002-10-01 18:41 UTC (permalink / raw)
  To: test test <dcmkx10@yahoo.com>; pgsql-php

Hi Sean, 

see below!

2002. október 1. 18:54 dátummal test test ezt írta:
| Hi,
|
| I am trying to insert a date into a date field, but also sometimes I
| need to insert a null value.  Inserting the null value seems to
| require not using quotes around null, but if I try to enter a date
| without quotes around it, it thinks it is a number.
|
| Here is the error I get: Warning: PostgreSQL query failed: ERROR:
| column "spec_start_date" is of type 'date' but expression is of type
| 'integer' You will need to rewrite or cast the expression
|
| The value I am sending is: spec_start_date = 10/01/02
|
| If I surround the date with single quotes it should be fine, but I
| can't do that when I want to insert a null value.  I know that I can
| do an "IF <use quotes> ELSE <don't use quotes>" in the SQL string, by
| I am entering many values and this would really get messy. I was
| hoping that there was a better solution. Thanks for any help.
|
| Sean
|

Beware crude hack is coming!!!

I wrote a fairly plain function which does the dirty quoting to any 
kind of values to be inserted. It looks like:


function quote($var, $type, $empty_null = TRUE)
{
  if ( empty($var) && $empty_null ){
    return 'NULL';
  }
  // if type is prepended with _ it means "array of ..."
  if ( $type{0} == '_' ){
    $type = substr($type, 1);
    $temp = "'{";
    foreach ( $var as $item ){
      $temp .= quote($item, $type).','; // recursive call
    }
    $temp = substr($temp, 0, -1);
    return $temp ."}'";
  }
  switch ( $type ){
  case 'int':
    return intval($var);
  case 'bool':
    return ($var ? "'t'::bool": "'f'::bool");
  case 'date':
    if ( is_numeric($var) ) { // treats as timestamp integer
      return date("'Y-m-d'", $var);
    }
  // otherwise as a date string
  case 'text':
    if (!get_magic_quotes_gpc()) {
      $var = addslashes($var);
    }
    return '\''.$var.'\'';
  default: trigger_error("unknown type : $type ($value)", E_USER_ERROR);
  }
}

You must notice that $var holds the corresponding value to be inserted 
or not set (in this case the function returns with NULL), $type is 
neither a legal type from PHP nor from Postgresql, but I think you can 
figure out how it works.

In my app, I have a PHP array which keeps information about what fields 
needs to be inserted and what type these fields have.


$PROPERTIES = array (
'seatnum'     => 'int',
'myear'       => 'int',
'kmage'       => 'int',
'price'       => 'int',
'volume'      => 'int',
'power'       => 'int',
'pprpm'       => 'int',
'color'       => 'text',
'damaged'     => 'bool',
'condition'   => 'int',
'ownernum'    => 'int',
'regvalid'    => 'text',
'cylindernum' => 'int',
'cylinderarr' => 'int',
'gearnum'     => 'int',
'gearing'     => 'int',
'autoclutch'  => 'bool',
'truss'       => 'int',
'trussmaterial' => 'int',
'frontbreak'    => 'int',
'rarebreak'   => 'int',
'accessories' => '_int',
'notes'       => 'text'
);

finally I have to loop through this array  and simply calls quote to 
quote the posted variables.

hth,
-- 
Papp, Győző
- pgerzson@freestart.hu



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 19:12  Sean <dcmkx10@yahoo.com>
  parent: Eckhard Hoeffner <e-hoeffner@fifoost.org>
  0 siblings, 1 reply; 8+ messages in thread

From: Sean @ 2002-10-01 19:12 UTC (permalink / raw)
  To: Eckhard Hoeffner <e-hoeffner@fifoost.org>; pgsql-php


Actually, this does not work, it generates the SQL fine, but I get a Parse error when trying to insert it. I am doing something very similar, did I mess up somewhere. 
Here is the PHP: 
if ($special_start_date == '')
{$special_start_date = 'null';}
else {$special_start_date = "'$special_start_date'";} 
if ($special_end_date == '')
{$special_end_date = 'null';}
else {$special_end_date = "'$special_end_date'";} 
$sql = "INSERT INTO prod_opt (
   xref_prod_id,
   code,
   title,
   isbn,
   list_price,
   our_price,
   spec_price,
   spec_start_date,
   spec_end_date,
   quantity,
   clearance,
   priority)
  VALUES (
   '".$prod_id."',
   '".$item_code."',
   '".$option_format."',
   '".$isbn."',
   ".$list_price.",
   ".$our_price.",
   ".$special_price.",
   ".$special_start_date.",
   ".$special_end_date.",
   ".$qty.",
   '".$clearance."',
   ".$priority.")";
$result = pg_exec($dbh, $sql); 
This is the error I get: Warning: PostgreSQL query failed: ERROR: parser: parse error at or near "10" 
This is the SQL generated: UPDATE prod_opt SET xref_prod_id = '4', code = '2', title = 'test', isbn = '2', list_price = 2, our_price = 2, spec_price = 2, spec_start_date = '10/01/02', spec_end_date = '10/01/02', quantity = 2, clearance = 't', priority = 2 WHERE id = '2' 
-Sean 
 Eckhard Hoeffner wrote: * Keary Suska [01 10 02 19:37]:

>on 10/1/02 10:54 AM, dcmkx10@yahoo.com purportedly said:
>
>I am trying to insert a date into a date field, but also sometimes I need to
>insert a null value. Inserting the null value seems to require not using
>quotes around null, but if I try to enter a date without quotes around it,
>it thinks it is a number.
>
>Here is the error I get: Warning: PostgreSQL query failed: ERROR: column
>"spec_start_date" is of type 'date' but expression is of type 'integer' You
>will need to rewrite or cast the expression
>
>If I surround the date with single quotes it should be fine, but I can't do
>that when I want to insert a null value. I know that I can do an "IF >quotes> ELSE " in the SQL string, by I am entering many
>values and this would really get messy. I was hoping that there was a better
>solution. Thanks for any help.
>
>If there is a way, I haven't found it yet. But it doesn't have to be
>messy--simply create a function that does the if-else for you, and your code
>will stay clean.

Maybe you should do the following. At the point where the switch 
"date" or "NULL" in the php-file is: 

if(empty($form_date)){
$date_var = NULL;
}else $date_var = "'$date'";

$query = "INSERT INTO table (table_row1, date) 
VALUES ('$value1', $date_var);";

You still have the if/else, but I think there must be somewhere the
switch NULL <=> date 

-- 
Eckhard Höffner
e-hoeffner@fifoost.org
D-80331 München
Tel. +49-89-21 03 18 88 

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 19:20  Sean <dcmkx10@yahoo.com>
  parent: Sean <dcmkx10@yahoo.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Sean @ 2002-10-01 19:20 UTC (permalink / raw)
  To: pgsql-php


Sorry, I pasted the wrong code. This is the PHP Code for the UPDATE not INSERT:
$sql = "UPDATE prod_opt
  SET xref_prod_id = '$prod_id',
   code = '$item_code',
   title = '$option_format',
   isbn = '$isbn',
   list_price = $list_price,
   our_price = $our_price,
   spec_price = $special_price,
   spec_start_date = $special_start_date,
   spec_end_date = $special_end_date,
   quantity = $qty,
   clearance = '$clearance',
   priority = $priority
  WHERE id = '$id'";
$result = pg_exec($dbh, $sql);
 Sean wrote:
Actually, this does not work, it generates the SQL fine, but I get a Parse error when trying to insert it. I am doing something very similar, did I mess up somewhere. 
Here is the PHP: 
if ($special_start_date == '')
{$special_start_date = 'null';}
else {$special_start_date = "'$special_start_date'";} 
if ($special_end_date == '')
{$special_end_date = 'null';}
else {$special_end_date = "'$special_end_date'";} 
$sql = "INSERT INTO prod_opt (
   xref_prod_id,
   code,
   title,
   isbn,
   list_price,
   our_price,
   spec_price,
   spec_start_date,
   spec_end_date,
   quantity,
   clearance,
   priority)
  VALUES (
   '".$prod_id."',
   '".$item_code."',
   '".$option_format."',
   '".$isbn."',
   ".$list_price.",
   ".$our_price.",
   ".$special_price.",
   ".$special_start_date.",
   ".$special_end_date.",
   ".$qty.",
   '".$clearance."',
   ".$priority.")";
$result = pg_exec($dbh, $sql); 
This is the error I get: Warning: PostgreSQL query failed: ERROR: parser: parse error at or near "10" 
This is the SQL generated: UPDATE prod_opt SET xref_prod_id = '4', code = '2', title = 'test', isbn = '2', list_price = 2, our_price = 2, spec_price = 2, spec_start_date = '10/01/02', spec_end_date = '10/01/02', quantity = 2, clearance = 't', priority = 2 WHERE id = '2' 
-Sean 
 Eckhard Hoeffner wrote: * Keary Suska [01 10 02 19:37]:

>on 10/1/02 10:54 AM, dcmkx10@yahoo.com purportedly said:
>
>I am trying to insert a date into a date field, but also sometimes I need to
>insert a null value. Inserting the null value seems to require not using
>quotes around null, but if I try to enter a date without quotes around it,
>it thinks it is a number.
>
>Here is the error I get: Warning: PostgreSQL query failed: ERROR: column
>"spec_start_date" is of type 'date' but expression is of type 'integer' You
>will need to rewrite or cast the expression
>
>If I surround the date with single quotes it should be fine, but I can't do
>that when I want to insert a null value. I know that I can do an "IF >quotes> ELSE " in the SQL string, by I am entering many
>values and this would really get messy. I was hoping that there was a better
>solution. Thanks for any help.
>
>If there is a way, I haven't found it yet. But it doesn't have to be
>messy--simply create a function that does the if-else for you, and your code
>will stay clean.

Maybe you should do the following. At the point where the switch 
"date" or "NULL" in the php-file is: 

if(empty($form_date)){
$date_var = NULL;
}else $date_var = "'$date'";

$query = "INSERT INTO table (table_row1, date) 
VALUES ('$value1', $date_var);";

You still have the if/else, but I think there must be somewhere the
switch NULL <=> date 

-- 
Eckhard Höffner
e-hoeffner@fifoost.org
D-80331 München
Tel. +49-89-21 03 18 88 

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!


---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Inserting Null Value or String Value
@ 2002-10-01 22:48  Keary Suska <hierophant@pcisys.net>
  parent: Sean <dcmkx10@yahoo.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Keary Suska @ 2002-10-01 22:48 UTC (permalink / raw)
  To: pgsql-php

on 10/1/02 1:20 PM, dcmkx10@yahoo.com purportedly said:

> This is the SQL generated: UPDATE prod_opt SET xref_prod_id = '4', code = '2',
> title = 'test', isbn = '2', list_price = 2, our_price = 2, spec_price = 2,
> spec_start_date = '10/01/02', spec_end_date = '10/01/02', quantity = 2,
> clearance = 't', priority = 2 WHERE id = '2'
> 
> This is the error I get: Warning: PostgreSQL query failed: ERROR: parser:
> parse error at or near "10"

I wouldn't expect that error message, but unless you have changed your date
preferences the date format being used is invalid. By default, you must use
SQl formatted dates, e.g. 2002-10-01

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2002-10-01 22:48 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-10-01 16:54 Inserting Null Value or String Value test test <dcmkx10@yahoo.com>
2002-10-01 17:37 ` Keary Suska <hierophant@pcisys.net>
2002-10-01 18:00   ` Roland Roberts <roland@astrofoto.org>
2002-10-01 18:08   ` Eckhard Hoeffner <e-hoeffner@fifoost.org>
2002-10-01 19:12     ` Sean <dcmkx10@yahoo.com>
2002-10-01 19:20       ` Sean <dcmkx10@yahoo.com>
2002-10-01 22:48         ` Keary Suska <hierophant@pcisys.net>
2002-10-01 18:41 ` Papp Gyozo <pgerzson@freestart.hu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox