From: Rod Kreisler <rod@23net.net>
To: Dan Jewett <danjewett@mac.com>
To: pgsql-php@postgresql.org
Subject: Re: empty form fields, NULLS, quoting, postgreSQL
Date: Fri, 28 Mar 2003 12:21:35 -0500
Message-ID: <5.2.0.9.0.20030328121631.01b16e30@127.0.0.1> (raw)
In-Reply-To: <20030328121907666343.GyazMail.danjewett@mac.com>
At 12:19 PM 3/28/2003 -0500, 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.
In addition to setting NULL you need to add the quotes to the string if
it's valid. That way you don't add them in the query itself to avoid
quoting NULL. Also, you shouldn't be using empty() for this. Try this:
$trk_length = strlen($_POST['trk_length'])==0 ? NULL :
"'".$_POST['trk_length']."'";
$result=pg_query($conn, "INSERT INTO track (field1, field2, trk_length)
VALUES ('$var1', '$var2', $trk_length)");
HTH
Rod
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: rod@23net.net, danjewett@mac.com
Subject: Re: empty form fields, NULLS, quoting, postgreSQL
In-Reply-To: <5.2.0.9.0.20030328121631.01b16e30@127.0.0.1>
References: <20030328121907666343.GyazMail.danjewett@mac.com> <5.2.0.9.0.20030328121631.01b16e30@127.0.0.1>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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