edpatterson Posted June 21, 2011 Share Posted June 21, 2011 I have a form with a dynamically created table, one of the cells is a text field. The cell name is 'item-' with a numeric value appended (item-1, item-2,...,item-87). Not all of the cells will contain a value, all values are supposed to be integers, through no more that 99999. I thought I could simply step through the $_POST data and only process the elements that had a value. Works just fine as long as I don't enter a 0. Problem is 0 is a valid entry. item-18 has a value of 0 but a update line is not being created. What I don't get is at the bottom the is_numeric(0) returns true (1). Here is my output, code I am using follows item-1 is the key 1 is the value update items set itemCount = 1 where itemID=1 item-5 is the key 3 is the value update items set itemCount = 3 where itemID=5 item-6 is the key 5 is the value update items set itemCount = 5 where itemID=6 item-7 is the key 7 is the value update items set itemCount = 7 where itemID=7 item-11 is the key 9 is the value update items set itemCount = 9 where itemID=11 item-12 is the key 2 is the value update items set itemCount = 2 where itemID=12 item-13 is the key 4 is the value update items set itemCount = 4 where itemID=13 item-16 is the key 6 is the value update items set itemCount = 6 where itemID=16 item-17 is the key 8 is the value update items set itemCount = 8 where itemID=17 item-18 is the key 0 is the value item-19 is the key is the value ... 0 1 <?php if(is_array($_POST)){ foreach($_POST as $key => $value){ echo $key.' is the key '.$value.' is the value<br />'; if((isset($key)) && (!$value == '')){ if(is_numeric($value)) { // echo $value.' is a number<br />'."\n"; $arrItems = explode("-",$key); echo 'update items set itemCount = '.$value.' where itemID='.$arrItems[1]."<br />\n"; } } } } else { echo '$_POST is not an array....'; } echo $_POST["item-18"]."<br />\n"; echo is_numeric(0)."<br />\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/ Share on other sites More sharing options...
Alex Posted June 21, 2011 Share Posted June 21, 2011 0 is different from "0" (string). Any data from a form will be a string. Instead of is_numeric which checks the datatype of the argument, you're looking for ctype_digit. I confused functions, nevermind. Solution in my next post.. Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/#findComment-1233056 Share on other sites More sharing options...
edpatterson Posted June 21, 2011 Author Share Posted June 21, 2011 Sigh, nope, same thing. Cells with a 0 in them do not generate an update line. In my own defense, the reason I used is_numeric is from the manual Note: To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric(). I must have read it bass ackwards. I thought it meant I had to use is_numeric instead of is_int. In either case 0's are still not working. Ed Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/#findComment-1233058 Share on other sites More sharing options...
Alex Posted June 21, 2011 Share Posted June 21, 2011 Sorry, I confused functions. The reason your code isn't working isn't because of the is_numeric() part at all, that's correct. The issue is here: if((isset($key)) && (!$value == '')){ namely the (!$value == '') portion. If $value == 0, then that statement will evaluate to false because 0 is evaluated to false, and !false == '' (or true == '') is false. Instead use: if((isset($key)) && $value != ""){ Alternatively, you could use: if((isset($key)) && !($value == '')){ Notice the position of the ! operator. Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/#findComment-1233059 Share on other sites More sharing options...
edpatterson Posted June 21, 2011 Author Share Posted June 21, 2011 Thanks a bazilion! It was driving me nuts. Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/#findComment-1233063 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 Keep in mind also that is_numeric will return TRUE for more than just decimal integers. 12E839 and 0xF3B24 would both be accepted as valid input . . . Quote Link to comment https://forums.phpfreaks.com/topic/240044-is_numeric-not-returning-true-for-0-in-form-data/#findComment-1233071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.