Jake Duncan
Jake Duncan

Reputation: 29

PHP MySQL Multiple Insertion

I'm not quite sure where I'm going wrong :(

if(isset($_POST['finish'])){
$objectives=addslashes(strip_tags($_POST['item']));

    foreach ($objectives AS $objective) {
        echo "$objective <br />";
    }

}

It's not showing anything.. what have I missed out? I'm trying to get data from multiple input entries..

<input class="item" id="objectives" name="item[]" />

Any ideas?

Upvotes: 0

Views: 223

Answers (2)

bgrohs
bgrohs

Reputation: 71

The direct answer is that any tag that has brackets([]) is put in the superglobal array as an array. You will need to loop over your this or use array_map to perform functions on this array.

My extended answer is that if you are using php 5.2 or later you can use the filter_var_array to perform this operation without iterating over your array in php. As well as do type checking. filter_var and filter_var_array have to many filter options for me to cover. Please see the docs http://php.net/manual/en/function.filter-var-array.php

if(isset($_POST['finish'])) {
    $objectives = filter_var_array($_POST['item'], FILTER_SANITIZE_STRING);

foreach ($objectives AS $objective) {
    echo "$objective <br />";
}

}

Upvotes: 0

km6zla
km6zla

Reputation: 4877

Well if you have multiple <input class="item" id="objectives" name="item[]" /> then $_POST['item'] will be an array and not a string. So you have to iterate over it or apply an array_map function.

$items = array_map('strip_tags',$items);
$items = array_map('addslashes',$items);

Your code would then be

if(isset($_POST['finish'])){
    $_POST['item'] = array_map('strip_tags',$_POST['item']);
    $_POST['item'] = array_map('addslashes',$_POST['item']);

    foreach ($_POST['item'] AS $objective) {
        echo "$objective <br />";
    }

}

Upvotes: 5

Related Questions