NetGamers.org

NetGamers.Org - The Premier Online Gaming Network
It is currently Sun Sep 05, 2010 6:50 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: PHP, NULL vals in MySQL, and web forms.
PostPosted: Thu Oct 07, 2004 12:42 am 
Offline
IRC Operator

Joined: Sun Jul 21, 2002 4:12 pm
Posts: 291
Location: UK
Hello.

[edit: much code here written as an example, so not tested, but you get the idea!]

I have a (large) table which (amongst other things) stores Yes/No/Maybe from a web form. The form goes a little like this:

Code:
Yes <input type="radio" name="foo" value="1">
No <input type="radio" name="foo" value="0">
Maybe <input type="radio" name="foo" value="2">

Yes <input type="radio" name="foo2" value="1">
No <input type="radio" name="foo2" value="0">
Maybe <input type="radio" name="foo2" value="2">


Now, in the DB the field default is set to NULL. So how come if the user submits the form without selecting anything (perfectly okay), it still records 0 in the DB? I would have thought it would remain as NULL. Also I tried setting the default to 9 (so Not NULL) as a test, 0 still gets inserted. I'm assuming it's a prob with the query I build in PHP, thus...

Code:
$foo = $_POST['foo'];
$foo2 = $_POST['foo2'];

$query = "INSERT INTO `footable` (`foo`, `foo2`) VALUES ('$foo', '$foo2');";


If I tick yes/no for the input foo but not foo2 then do print_r($_POST), it only tells me that foo exists. Because of course, foo2 was never set, so it doesn't exist in the $_POST array. So the annoying thing is that if I do echo $query, I get the following:
Code:
INSERT INTO footable (foo, foo2) VALUES ('1', '');
So I'd assume that literally nothing would be inserted into the DB field called foo2?
Can anybody shed some light on this? It's REALLY pissing me off :D If you can think of a solution or easy hack, then let me know please! It's taken months to develop this set of forms, but the "bug" only just became an issue, and I have to deliver on Friday! ::rolleyes: :rolleyes:

Ta muchly,

M.

_________________
Former NG/PA busybody.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 07, 2004 6:38 pm 
Offline
Services Coder

Joined: Sat Nov 17, 2001 12:00 am
Posts: 616
Location: Behind You
The answer is in SQL. '' is equal to 0 for a numeric field. If you want NULL, you need to insert NULL, or not insert that field at all, ie

INSERT INTO table (field1) VALUES ($value1)

Remember - in SQL, _nothing_ is equal to NULL. Especially not ''.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group