Jump to content

Recommended Posts

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";
?>

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..

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.