From dcmkx10@yahoo.com Tue Oct 1 16:54:48 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 24648476567 for ; Tue, 1 Oct 2002 12:54:48 -0400 (EDT) Received: from web14410.mail.yahoo.com (web14410.mail.yahoo.com [216.136.173.221]) by postgresql.org (Postfix) with SMTP id 718764764CD for ; Tue, 1 Oct 2002 12:54:47 -0400 (EDT) Message-ID: <20021001165453.882.qmail@web14410.mail.yahoo.com> Received: from [64.1.187.234] by web14410.mail.yahoo.com via HTTP; Tue, 01 Oct 2002 09:54:53 PDT Date: Tue, 1 Oct 2002 09:54:53 -0700 (PDT) From: test test Subject: Inserting Null Value or String Value To: pgsql-php@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1322507064-1033491293=:761" X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/1 X-Sequence-Number: 1516 --0-1322507064-1033491293=:761 Content-Type: text/plain; charset=us-ascii 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 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. Sean --------------------------------- Do you Yahoo!? New DSL Internet Access from SBC & Yahoo! --0-1322507064-1033491293=:761 Content-Type: text/html; charset=us-ascii

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! --0-1322507064-1033491293=:761-- From hierophant@pcisys.net Tue Oct 1 17:37:10 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id D7E254765E5 for ; Tue, 1 Oct 2002 13:37:10 -0400 (EDT) Received: from marsha.pcisys.net (marsha.pcisys.net [216.229.32.146]) by postgresql.org (Postfix) with ESMTP id 60CFA4763DE for ; Tue, 1 Oct 2002 13:37:06 -0400 (EDT) Received: from [208.202.106.238] (moe1.meg106-238.cos.pcisys.net [208.202.106.238]) by marsha.pcisys.net (8.12.5/8.12.5) with ESMTP id g91Hb46u024607 for ; Tue, 1 Oct 2002 11:37:04 -0600 (MDT) User-Agent: Microsoft-Outlook-Express-Macintosh-Edition/5.0.4 Date: Tue, 01 Oct 2002 11:37:04 -0600 Subject: Re: Inserting Null Value or String Value From: Keary Suska To: Postgres-PHP Message-ID: In-Reply-To: <20021001165453.882.qmail@web14410.mail.yahoo.com> Mime-version: 1.0 Content-type: multipart/alternative; boundary="MS_Mac_OE_3116317025_10963218_MIME_Part" X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/2 X-Sequence-Number: 1517 > This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --MS_Mac_OE_3116317025_10963218_MIME_Part Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit 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 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. Keary Suska Esoteritech, Inc. "Leveraging Open Source for a better Internet" --MS_Mac_OE_3116317025_10963218_MIME_Part Content-type: text/html; charset="US-ASCII" Content-transfer-encoding: quoted-printable Re: [PHP] Inserting Null Value or String Value 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 someti= mes 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 &q= uot;spec_start_date" is of type 'date' but expression is of type 'inte= ger' You will need to rewrite or cast the expression

The value I am sending is: spec_start_date =3D 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 &qu= ot;IF <use quotes> ELSE <don't use quotes>" in the SQL str= ing, by I am entering many values and this would really get messy. I was ho= ping 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"
--MS_Mac_OE_3116317025_10963218_MIME_Part-- From roland@astrofoto.org Tue Oct 1 18:00:08 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DC0C0475CB4 for ; Tue, 1 Oct 2002 14:00:08 -0400 (EDT) Received: from mail.astrofoto.org (dsl092-102-177.nyc2.dsl.speakeasy.net [66.92.102.177]) by postgresql.org (Postfix) with ESMTP id BA822476866 for ; Tue, 1 Oct 2002 14:00:07 -0400 (EDT) Received: from kuiper.rlent.pnet (IDENT:root@kuiper.rlent.pnet [192.168.3.50]) by mail.astrofoto.org (8.12.3/8.12.3) with ESMTP id g91I07UZ008615 for ; Tue, 1 Oct 2002 14:00:07 -0400 Received: (from roland@localhost) by kuiper.rlent.pnet (8.11.6/8.11.6) id g91I07P03840; Tue, 1 Oct 2002 14:00:07 -0400 X-Authentication-Warning: kuiper.rlent.pnet: roland set sender to roland@astrofoto.org using -f To: Postgres-PHP Subject: Re: Inserting Null Value or String Value References: From: Roland Roberts Date: 01 Oct 2002 14:00:07 -0400 In-Reply-To: Message-ID: Lines: 18 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2.90 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/3 X-Sequence-Number: 1518 >>>>> "Keary" == Keary Suska 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 ELSE Keary> " 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 From e-hoeffner@fifoost.org Tue Oct 1 18:08:30 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 85159475EBB for ; Tue, 1 Oct 2002 14:08:30 -0400 (EDT) Received: from mailout06.sul.t-online.com (mailout06.sul.t-online.com [194.25.134.19]) by postgresql.org (Postfix) with ESMTP id E5ED947679B for ; Tue, 1 Oct 2002 14:08:28 -0400 (EDT) Received: from fwd01.sul.t-online.de by mailout06.sul.t-online.com with smtp id 17wRRc-0002hg-02; Tue, 01 Oct 2002 20:08:28 +0200 Received: from localhost (520054136294-0001@[80.128.17.173]) by fmrl01.sul.t-online.com with esmtp id 17wRRP-0FioNMC; Tue, 1 Oct 2002 20:08:15 +0200 Received: from eckhard by localhost with local (Exim 3.35 #1 (Debian)) id 17wRRD-0000Jx-00 for ; Tue, 01 Oct 2002 20:08:03 +0200 Date: Tue, 1 Oct 2002 20:08:03 +0200 From: Eckhard Hoeffner To: Postgres-PHP Subject: Re: Inserting Null Value or String Value Message-ID: <20021001180803.GA17732@fifoost.org> Mail-Followup-To: Postgres-PHP References: <20021001165453.882.qmail@web14410.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: User-Agent: Mutt/1.4i X-Mailer: Mutt http://www.mutt.org/ Organization: FiFo Ost =?iso-8859-1?Q?M=FCnchen?= X-Editor: Vim http://www.vim.org/ X-URL: http://www.fifoost.org X-Uptime: 19:53:08 up 12:02, 1 user, load average: 0.23, 0.32, 0.24 X-Sender: 520054136294-0001@t-dialin.net X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/4 X-Sequence-Number: 1519 * 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 bett= er >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 co= de >will stay clean. Maybe you should do the following. At the point where the switch=20 "date" or "NULL" in the php-file is:=20 if(empty($form_date)){ $date_var =3D NULL; }else $date_var =3D "'$date'"; $query =3D "INSERT INTO table (table_row1, date)=20 VALUES ('$value1', $date_var);"; You still have the if/else, but I think there must be somewhere the switch NULL <=3D> date=20=20=20=20=20=20=20=20=20=20 --=20 Eckhard H=F6ffner e-hoeffner@fifoost.org D-80331 M=FCnchen Tel. +49-89-21 03 18 88=20 From pgerzson@freestart.hu Tue Oct 1 18:40:36 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C104E476536 for ; Tue, 1 Oct 2002 14:40:36 -0400 (EDT) Received: from hapcibeno.freestart.hu (hapcibeno.freestart.hu [213.197.64.8]) by postgresql.org (Postfix) with ESMTP id 133BE4764CD for ; Tue, 1 Oct 2002 14:40:36 -0400 (EDT) Received: from line-68-198.dial.freestart.hu ([213.197.68.198] helo=there) by freestart.hu with smtp (Freestart relay 2 (FS-Lin)) id 17wSFC-0003rO-00; Tue, 01 Oct 2002 20:59:42 +0200 Content-Type: text/plain; charset="iso-8859-2" From: "Papp Gyozo" Reply-To: pgerzson@freestart.hu To: test test , pgsql-php@postgresql.org Subject: Re: Inserting Null Value or String Value Date: Tue, 1 Oct 2002 20:41:57 +0200 X-Mailer: KMail [version 1.3.1] References: <20021001165453.882.qmail@web14410.mail.yahoo.com> In-Reply-To: <20021001165453.882.qmail@web14410.mail.yahoo.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: X-Scanner: exiscan by Freestart X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/5 X-Sequence-Number: 1520 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 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. | | 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 From dcmkx10@yahoo.com Tue Oct 1 19:12:43 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 91F6C4765F9 for ; Tue, 1 Oct 2002 15:12:43 -0400 (EDT) Received: from web14410.mail.yahoo.com (web14410.mail.yahoo.com [216.136.173.221]) by postgresql.org (Postfix) with SMTP id EA0734765DC for ; Tue, 1 Oct 2002 15:12:42 -0400 (EDT) Message-ID: <20021001191243.28811.qmail@web14410.mail.yahoo.com> Received: from [64.1.187.234] by web14410.mail.yahoo.com via HTTP; Tue, 01 Oct 2002 12:12:43 PDT Date: Tue, 1 Oct 2002 12:12:43 -0700 (PDT) From: Sean Subject: Re: Inserting Null Value or String Value To: Eckhard Hoeffner , Postgres-PHP In-Reply-To: <20021001180803.GA17732@fifoost.org> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1846274692-1033499563=:28434" X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/6 X-Sequence-Number: 1521 --0-1846274692-1033499563=:28434 Content-Type: text/plain; charset=us-ascii 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! --0-1846274692-1033499563=:28434 Content-Type: text/html; charset=us-ascii

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! --0-1846274692-1033499563=:28434-- From dcmkx10@yahoo.com Tue Oct 1 19:20:10 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id A184C475FBA for ; Tue, 1 Oct 2002 15:20:10 -0400 (EDT) Received: from web14405.mail.yahoo.com (web14405.mail.yahoo.com [216.136.174.62]) by postgresql.org (Postfix) with SMTP id C34F3475EBB for ; Tue, 1 Oct 2002 15:20:09 -0400 (EDT) Message-ID: <20021001192010.91020.qmail@web14405.mail.yahoo.com> Received: from [64.1.187.234] by web14405.mail.yahoo.com via HTTP; Tue, 01 Oct 2002 12:20:10 PDT Date: Tue, 1 Oct 2002 12:20:10 -0700 (PDT) From: Sean Subject: Re: Inserting Null Value or String Value To: Postgres-PHP In-Reply-To: <20021001191243.28811.qmail@web14410.mail.yahoo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1322220705-1033500010=:90691" X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/7 X-Sequence-Number: 1522 --0-1322220705-1033500010=:90691 Content-Type: text/plain; charset=us-ascii 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! --0-1322220705-1033500010=:90691 Content-Type: text/html; charset=us-ascii

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! --0-1322220705-1033500010=:90691-- From hierophant@pcisys.net Tue Oct 1 22:48:09 2002 Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 32B384768E7 for ; Tue, 1 Oct 2002 18:48:09 -0400 (EDT) Received: from marsha.pcisys.net (marsha.pcisys.net [216.229.32.146]) by postgresql.org (Postfix) with ESMTP id 8BF124768CE for ; Tue, 1 Oct 2002 18:48:07 -0400 (EDT) Received: from [208.202.106.238] (moe1.meg106-238.cos.pcisys.net [208.202.106.238]) by marsha.pcisys.net (8.12.5/8.12.5) with ESMTP id g91Mm76u009218 for ; Tue, 1 Oct 2002 16:48:08 -0600 (MDT) User-Agent: Microsoft-Outlook-Express-Macintosh-Edition/5.0.4 Date: Tue, 01 Oct 2002 16:48:09 -0600 Subject: Re: Inserting Null Value or String Value From: Keary Suska To: Postgres-PHP Message-ID: In-Reply-To: <20021001192010.91020.qmail@web14405.mail.yahoo.com> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/8 X-Sequence-Number: 1523 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"