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--