Forum Moderators: coopster
$nextq = (int)$_POST['nextquest'];
if (eregi([0-9], $nextq))
{
//INSERT $nextq INTO THE DATABASE
}
else
{
//DISPLAY ERROR IDENTIFYING $nextq AS A NON INT
}
can someone tell me if my if statement looks right and perhaps if i could do it like this..
if (!eregi([0-9], $nextq))
sort of like if nextq is not an int
any help here would be appreciated
Specially on "Some special character sequences" block.
[edited by: Psychopsia at 6:04 pm (utc) on Feb. 12, 2007]
$string = '5';
if (ctype_digit($string))
{
//ok
}
if (ereg("^[[:digit:]]+$", $string))
{
//ok
}
dc
if (preg_match('/^\d$/', $nextq) && preg_match('/^\d$/', $weight))
{
// SQL
}
else
{
//ERROR
}
it doesnt work if enter the number 10 in any of the fields e.g.
$nextq = 7
$weight = 10
i get an error with the above if statement and the values, I tried
if (ctype_digit($nextq) && ctype_digit($weight))
{
//SQL
}
else
{
//ERROR
}
I found out that ctype_digit works however the reason it wasnt working before was because i was testing $nextq
and $nextq = (int)$POST['next_qid'];
the (int) was preventing ctype_digit from working
you should never (I've only used it once in a production script) need to typecast vars in php, even though types are fluid in php they work well. If you are always sure what type of data you are passing around you'll be ine.
As you can see with your example, you created a situation where you changed data before you tested it which gave wrong results.
and my apologies, I saw that when I first read this thread and should have said something.