0

My $_POST array is like this:

array(4){
  [4]=> string(0) ""
  [1]=> string(1) "5"
  [2]=> string(1) "3"
  [3]=> string(0) "" 
}

The array keys stand for the ID of the database entry. So I want to try to use the keys for ids to update my database.

for($db=0; $db<count($_POST); $db++){
    if($_POST[$db] != NULL){
        $id = key(current($_POST[$db])); //<-- Problem 
        $value = $_POST[$db];
        // query code
    }
}

The line with $id doesn't work. Can someone help me?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

1

Here we are iterating over this $_POST using foreach, hope this will help you out.

Change this to:

for($db=0; $db<count($_POST); $db++){
    if($_POST[$db] != NULL){
    $id = key(current($_POST[$db])); <-- Problem 
    $value = $_POST[$db]));

    --- here follows the query   ---
    }

This:

<?php
if(is_array($_POST) && count($_POST)>0)
{
    foreach($_POST as $id=> $value)
    {
    //$id is your id
    //$value is the value on that id    
    //--- here follows the query   ---
    }
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

why dont you use foreach()

foreach($db as $key => $value){
       $key is the id you need
}
Exprator
  • 26,992
  • 6
  • 47
  • 59
0

Rather than running a conditional check on every iteration of the $_POST array, better practice would be to filter $_POST before looping, then you can use your prepared statement query (for security reasons) on every iteration without any conditional expressions.

It seems from your sample input that you are expecting either empty values or numeric values. Also, it is very common for database table structures to use auto-incremented id's that start from 1. I'll provide 3 different methods, so that you can choose the most effective one for your project. All methods preserve the original key (id) in $_POST after filtering.

Method #1: (remove all null, empty, false-y, zero-ish elements from $_POST)

foreach(array_filter($_POST) as $id=>$value){
    // insert your prepared statement query code block here
}

Method #2: (only permit elements from $_POST with a value greater than zero)

foreach(array_filter($_POST,function($v){return $v>0;}) as $id=>$value){
    // insert your prepared statement query code block here
}

Method #3: (only permit elements from $_POST with a value that is an integer)

foreach(array_filter($_POST,function($v){return is_int($v);}) as $id=>$value){
    // insert your prepared statement query code block here
}

Here is a demonstration of all three methods.

Furthermore, there are some unnecessary checks in Sahil's answer.

First, checking if(is_array($_POST)) will always give a true result because $_POST is a superglobal array. If you don't believe me, you can trust deceze's comment here as he's got one of those fancy diamonds after his username. The exception being if you are using Content-Type:text/xml, but surely that is not the case this time.

Second, checking the count() on $_POST is unnecessary as the foreach() loop will not even iterate once if the array is empty.

Community
  • 1
  • 1
mickmackusa
  • 43,625
  • 12
  • 83
  • 136