Re: empty form fields, NULLS, quoting, postgreSQL inserts into

From: Vince Vielhaber <vev(at)michvhf(dot)com>
To: Dan Jewett <danjewett(at)mac(dot)com>
Cc: pgsql-php(at)postgresql(dot)org
Subject: Re: empty form fields, NULLS, quoting, postgreSQL inserts into
Date: 2003-03-28 17:21:09
Message-ID: Pine.BSF.4.44.0303281217380.34973-100000@paprika.michvhf.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

On Fri, 28 Mar 2003, Dan Jewett wrote:

> I'm having trouble getting NULLs into date/time fields and interval
> fields.
>
> I've gotten this far:
>
> $trk_length = $_POST['trk_length'];
> if (empty($trk_length)) //or if($trk_length == '')
> $trk_length = NULL;
>
> or
>
> $length_str = $_POST['trk_length'];
> if (empty($length_str))
> $trk_length = NULL;
> else $trk_length = $length_str;
>
> The insert:
>
> $result = pg_query($conn, "INSERT INTO track (field1, field2,
> trk_length) VALUES ('$var1', '$var2', '$trk_length')");
>
> This results in a "bad external representation ''." error for the
> insert. If I use double quotes, ie. $trk_length = "NULL"; and remove
> the single quotes from the $trk_length variable in the insert
> statement, the insert succeeds. But now, if $trk_length is not empty,
> the insert fails with a parse error on the : character in my interval
> string.

embed the single quotes into $trk_length.

if(empty($trk_length))
$tkln = "NULL";
else
$tkln = "'$trk_length'";

... VALUES ('$var1', '$var2', $tkln)");

Vince.
--
Fast, inexpensive internet service 56k and beyond! http://www.pop4.net/
http://www.meanstreamradio.com http://www.unknown-artists.com
Internet radio: It's not file sharing, it's just radio.

In response to

Browse pgsql-php by date

  From Date Subject
Next Message Rod Kreisler 2003-03-28 17:21:35 Re: empty form fields, NULLS, quoting, postgreSQL
Previous Message Dan Jewett 2003-03-28 17:19:07 empty form fields, NULLS, quoting, postgreSQL inserts into date, interval fields...