* Thus wrote Jonas Thorell ([EMAIL PROTECTED]):
> Sorry, wrote wrong here so one more try.
> 
> Well, no. But that's not where the problem is. If I was unclear - the
> id-field work as it should. It's when I try to use A variable in the
> query-string the problem pops up.
> 
> IOW - INSERT INTO SITE (id,name,url,comment) VALUES
> (null,'testname','testurl',null)
> 
> Works
> 
> INSERT INTO SITE (id,name,url,comment) VALUES
> (null,'$name','$url','$comment')
> 
> Does not when I've set $comment=null;
> 
> If every field has a proper value (when everything had been filled in on the
> form), Everything works as expected.

The problem is when the string gets translated (variables expanded)
and sent to mysql is that the string looks like this:

INSERT INTO SITE (id,name,url,comment) VALUES
(null,'Name of url','http://blah/','')
 
So when the database sees the query it thinks you want to put and
empty sting there, not null.  To achieve what you want to do you
have to make sure that the word, NULL, without quotes around it,
gets sent to the databaase.

$comment2 = trim($comment); // also clear out space just in case
if (strlen($comment2)) {
  // escape the string
  $comment2 = mysql_escape_string($comment2);

  // wrap the string in single quotes for the db
  $comment2 = "'$comment2'";
} else {
  // send the keyword NULL to the database
  $comment2 = "null";
}
  
Now in your SQL statement you would put the $comment2 variable in
the proper place without the single quotes:

INSERT INTO SITE (id,name,url,comment) VALUES
(null,'$name','$url',$comment2)

Now your sql statment will be (if comment was left blank:)

INSERT INTO SITE (id,name,url,comment) VALUES
(null,'Name of url','http://blah/',null)
 
or if it had a value:

INSERT INTO SITE (id,name,url,comment) VALUES
(null,'Name of url','http://google.com/','This should be first')


HTH,

Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to