Page 1 of 1

Stopping negative numbers

Posted: Wed Feb 12, 2003 5:29 am
by megaming
Hi,

How do I stop users sending negative numbers to my database from a form?

I don't want to do this with javascript because that can easily be turned off.

Thanks :D

Posted: Wed Feb 12, 2003 5:32 am
by twigletmac
You can do a test on the form processing page that checks to see if a number is greater than zero:

Code: Select all

if ($_POST['number'] >= 0){
   echo 'Positive Number';
} else {
   echo 'Negative Number';
}
Mac

Posted: Wed Feb 12, 2003 5:33 am
by megaming
I've already got that, but it doesn't work...

Code: Select all

if ($newgold < "0") &#123;
print "You do not have that much!";
&#125;
if ($newgold > "0") &#123;
// do db stuff
&#125;

Posted: Wed Feb 12, 2003 5:40 am
by twigletmac
Have you tried something like:

Code: Select all

if ($_POST['newgold'] < 0) {
    echo 'You do not have that much!'; 
} else { 
    // do db stuff 
}
Mac

Posted: Wed Feb 12, 2003 5:46 am
by megaming
is there actually any difference?

Posted: Wed Feb 12, 2003 5:54 am
by twigletmac
Yes - first of all it uses the $_POST array to reference the variable just in case the problem is one caused by register_globals:
viewtopic.php?t=511

Secondly instead of using two if statements it uses one if...else statement which makes more sense as you can first test if the variable is less than 0 (ie. negative) and then if it's not the default stuff in the else statement is done.

Mac