User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 7 Oct 2004, 01:18   #1
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Forms, null values, and PHP/MySQL

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

Ta muchly,

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"

Last edited by Mong; 7 Oct 2004 at 01:40.
Mong is offline   Reply With Quote
Unread 7 Oct 2004, 02:07   #2
DarkAngel
Nothing Is Forever.
 
Join Date: Mar 2002
Location: Fallen From Grace.
Posts: 475
DarkAngel is an unknown quantity at this point
Re: Forms, null values, and PHP/MySQL

you know my opinion on it m8 - still insist its a db setup prob.
__________________
dead.
DarkAngel is offline   Reply With Quote
Unread 7 Oct 2004, 06:57   #3
Caesar2
Commander
 
Caesar2's Avatar
 
Join Date: Sep 2001
Location: Netherlands
Posts: 146
Caesar2 is just really niceCaesar2 is just really niceCaesar2 is just really niceCaesar2 is just really nice
Re: Forms, null values, and PHP/MySQL

Are the fields foo and foo2 number fields? I don't know MySQL, but from experience with other db's, I know that some convert an empty string to 0 in number fields.
You can check if all fields on the form are filled in before you insert it in the db, or set a default in the form
__________________
Quote:
Originally posted by Cochese
Cathaar are not overpowered.

You were just "bashed", live with it.
Caesar2 is offline   Reply With Quote
Unread 7 Oct 2004, 08:22   #4
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Forms, null values, and PHP/MySQL

previous answer scratched cos i didn't read the question properly

Ummmmmm, if it's a numerical field then NULL == 0, so it's actually doing what you ask it to.
I think so anyway.
try

Code:
.
.
.
$result = mysql_query("SELECT <relevant column> FROM <table> WHERE <relevant column> = 0");
$row = mysql_fetch_array($result);
if ($row[<relevant column>] == NULL) echo "Yep, 0 is NULL";
.
.
.
I have a feeling the echo will execute.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.

Last edited by meglamaniac; 7 Oct 2004 at 08:28.
meglamaniac is offline   Reply With Quote
Unread 7 Oct 2004, 08:29   #5
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Re: Forms, null values, and PHP/MySQL

Ta for the help. Problem is that I simply don't have the time to go through every form and write handlers for the radio not being checked - gotta go live at 4pm today! I'll think of something...

I think I'll kill my boss, who a year ago said "this is a little job, no need to spend ages writing a spec, just get on with it"
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 7 Oct 2004, 08:32   #6
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Forms, null values, and PHP/MySQL

Psst, refresh your page and look at the post again.
I misread the question and edited accordingly.

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 9 Oct 2004, 00:49   #7
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: Forms, null values, and PHP/MySQL

its too late now, but anyway try
if(isset($_POST['foo2']))
$foo2 = $_POST['foo2'];
else
$foo2 = 'NULL';

php doesnt care much if a string variable has no value, it just turns it into an empty string. mysql has no idea what to do with an empty string in a numeric field, so it turns it into 0 (for whatever reasons there may be )
i dont think 0 and NULL are the same thing, 0 is a value, NULL isnt. but im not really sure, expecially not with mysql
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 9 Oct 2004, 01:39   #8
TheRat
Retired
 
Join Date: Dec 2000
Location: Rogaland, Norway
Posts: 642
TheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud of
Re: Forms, null values, and PHP/MySQL

Whatever happened to the art of input checking? What if someone says "Hey, lets post some custom data to that form and delete the servers databases!"

Code:
 $_POST['foo'] = ( strlen( $_POST['foo'] ) > 0 ) 
                                     ? mysql_escape_string( $_POST['foo'] ) 
                                      : "default value";
Shame on you all!


[edit: Realized that php highlighting looks fked on this forum ]
__________________
Of all the things I've lost I miss my mind the most

-Elysium Officer
[1up] Senior MO
Retired
TheRat is offline   Reply With Quote
Unread 9 Oct 2004, 01:41   #9
TheRat
Retired
 
Join Date: Dec 2000
Location: Rogaland, Norway
Posts: 642
TheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud of
Re: Forms, null values, and PHP/MySQL

Also in reply to wu_trax:

Ternary operators > *
__________________
Of all the things I've lost I miss my mind the most

-Elysium Officer
[1up] Senior MO
Retired
TheRat is offline   Reply With Quote
Unread 9 Oct 2004, 09:17   #10
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: Forms, null values, and PHP/MySQL

yes, they are usefull sometimes, but in my opinion they make the code harder to read for other people.
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 9 Oct 2004, 17:25   #11
TheRat
Retired
 
Join Date: Dec 2000
Location: Rogaland, Norway
Posts: 642
TheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud ofTheRat has much to be proud of
Re: Forms, null values, and PHP/MySQL

Not if properly formated (logically formated)
__________________
Of all the things I've lost I miss my mind the most

-Elysium Officer
[1up] Senior MO
Retired
TheRat is offline   Reply With Quote
Unread 9 Oct 2004, 23:58   #12
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Forms, null values, and PHP/MySQL

Nowt wrong with t-ops.
Look at this common return situation:
if (value > constant) return TRUE;
else return FALSE;

Now re-written as:
return (value > constant) ? TRUE : FALSE;

How is that any less readable?
Obviously you have to KNOW what the ? and : do, but then the same goes for all the other operators in the language - so if you know the language whats the problem?
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 10 Oct 2004, 10:33   #13
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: Forms, null values, and PHP/MySQL

i only remember that i once had to change code of someone else and that someone included another file in such a statement. i had to search for ages before i found that.
with an if -statement and proper syntax-highlighting that would have been easier.
(but then, the whole code that someone wrote was horrible, maybe that was the problem)
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 12 Oct 2004, 19:13   #14
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
Post Re: Forms, null values, and PHP/MySQL

If they are numeric values... why on earth are you encapsulating them in single quotes in your query? If you want them treated as numbers, put them as numbers into the query (i.e. without quotation marks!).

NULL means no entry, 0 is not the same as NULL. Single quotes with nothing in between is also not the same as NULL. Would be kinda pointless having to explicity declare fields as being nullable, if they could take equivilent values anyway don't you think?

Quote:
Originally Posted by Big T-SQL Manual
The nullability of a column determines if the rows in the table can contain a null value for that column. A null value, or NULL, is not the same as zero (0), blank, or a zero-length character string such as ""; NULL means that no entry has been made. The presence NULL usually implies that the value is either unknown or undefined.
As for how PHP would write the unset var, I've no idea, as I dont use PHP.




And lastly for megla (I'm sure you must know this but) ...

Quote:
Originally Posted by meglamaniac
Nowt wrong with t-ops.
Look at this common return situation:
if (value > constant) return TRUE;
else return FALSE;

Now re-written as:
return (value > constant) ? TRUE : FALSE;

How is that any less readable?
They are both completely stupid. The result of the comparison is going to be a boolean anyway, just return the comparison. If (true) return true? Looks kinda silly no?

return (value > constant);

I guess you were probably just looking for an example to demonstrate the inline if operator, alas you chose a pap one.
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 12 Oct 2004, 20:28   #15
MAdnRisKy
home wrecker
 
MAdnRisKy's Avatar
 
Join Date: Jan 2001
Location: The other side of the galaxy ;)
Posts: 1,041
MAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to beholdMAdnRisKy is a splendid one to behold
Re: Forms, null values, and PHP/MySQL

Jesus raging you're still alive? (and still checking PA boards - heck the PROGRAMING BOARDS - whatever you were doing that you let this distract you from, it must have been toenail pullingly dull!)
__________________
May the Farce be with you...

#pr0nstars - a pimp is for life, not just for christmas
MAdnRisKy is offline   Reply With Quote
Unread 15 Oct 2004, 14:59   #16
sayonara
Shai Halud
 
sayonara's Avatar
 
Join Date: Aug 2001
Location: Sunny Leeds \o/
Posts: 2,127
sayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nice
Re: Forms, null values, and PHP/MySQL

Mongy Mong Mong. This comes about because for some reason '(nothing)' is not the same as NULL.


Use one of the suggested methods to check the input before you involve it in an SQL query.


If the omitted input is mandatory, send them back to the form.

If the input is non-mandatory and your default value is NULL, insert nothing for that field (assuming the field contains no old value).
If your default value is not NULL, or the user is updating an old value, then insert/update NULL instead of 'the_input'.
__________________
meow
sayonara is offline   Reply With Quote
Unread 15 Oct 2004, 15:51   #17
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Forms, null values, and PHP/MySQL

Quote:
Originally Posted by Raging.Retard
And lastly for megla (I'm sure you must know this but) ...
They are both completely stupid. The result of the comparison is going to be a boolean anyway, just return the comparison. If (true) return true? Looks kinda silly no?

return (value > constant);

I guess you were probably just looking for an example to demonstrate the inline if operator, alas you chose a pap one.
In order:
Yes.
Yes.
Yes.
Yes.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 21 Oct 2004, 22:50   #18
sayonara
Shai Halud
 
sayonara's Avatar
 
Join Date: Aug 2001
Location: Sunny Leeds \o/
Posts: 2,127
sayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nice
Re: Forms, null values, and PHP/MySQL

Quote:
Originally Posted by TheRat
Whatever happened to the art of input checking? What if someone says "Hey, lets post some custom data to that form and delete the servers databases!"

Code:
 $_POST['foo'] = ( strlen( $_POST['foo'] ) > 0 ) 
                                     ? mysql_escape_string( $_POST['foo'] ) 
                                      : "default value";
Oh yeah - I should probably mention that mysql_escape_string is deprecated.

Use this instead:
http://uk.php.net/manual/en/function...ape-string.php
__________________
meow
sayonara is offline   Reply With Quote
Unread 15 Nov 2004, 00:59   #19
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: Forms, null values, and PHP/MySQL

Quote:
Originally Posted by sayonara
Oh yeah - I should probably mention that mysql_escape_string is deprecated.

Use this instead:
http://uk.php.net/manual/en/function...ape-string.php

Do you even know why its deprecated?

Didnt see a shitload of binary data being inserted there mate.

rr, better example of ternary operator?
Code:
out << "This is the " << (badExample ? "worst example" : "stupidest post") << " ever"
    << std::endl;
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 16 Nov 2004, 13:55   #20
sayonara
Shai Halud
 
sayonara's Avatar
 
Join Date: Aug 2001
Location: Sunny Leeds \o/
Posts: 2,127
sayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nice
Re: Forms, null values, and PHP/MySQL

Quote:
Originally Posted by MT
Do you even know why its deprecated?

Didnt see a shitload of binary data being inserted there mate.
Does it matter?

This is a 'best practices' question. One does not use deprecated code.

Wow, I can't believe you bumped this thread for that.
__________________
meow
sayonara is offline   Reply With Quote
Unread 16 Nov 2004, 14:13   #21
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Forms, null values, and PHP/MySQL

I'm more of the opinion of "if it ain't broke don't fix it".
I couldn't be arsed to go through all my code changing the function used when the use of the depracated one has no impact because it's not taking binary data.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 16 Nov 2004, 14:52   #22
sayonara
Shai Halud
 
sayonara's Avatar
 
Join Date: Aug 2001
Location: Sunny Leeds \o/
Posts: 2,127
sayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nicesayonara is just really nice
Re: Forms, null values, and PHP/MySQL

Well, the idea with deprecation (in any language) is that it gives everyone the chance to switch their working practices over to the new method, so that when it finally drops out of the spec your applications don't all fall over. That means it's relatively effort-free as the deprecation period can last months if not years (and of course if you are using OOP it's a simple matter to change your code anyway).

MT's implication that Mong should use deprecated code but still remember to use the replacement function whenever he has binary data is a bit... weird, and frankly sloppy.
__________________
meow
sayonara is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 16:13.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018