0

Whats the problem with below code.Error Warning: Invalid argument supplied for foreach()

    if(isset($_POST['submit']))
        {
        foreach($_POST['team[]'] as $value)
        {
        $insert=mysql_query("INSERT INTO team('team') VALUES ('$value')");
        }
        if($insert)
        {
        echo "dONE";
        }
        }
<form method="post" action="check.php">
<input type="checkbox" name="team[]" value="AG"> Argentina
<input type="checkbox" name="team[]" value="GE"> Germany
<input type="checkbox" name="team[]" value="BR"> Brazil
<input type="submit" name="submit" value="submit">
</form>
1
  • I think you should use foreach($_POST['team'] as $value) Commented Jul 31, 2012 at 15:32

5 Answers 5

2

Try with:

foreach($_POST['team'] as $value)
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure? Have you tried? Just test it on my localhost and it works.
Invalid argument supplied for foreach()
can you give share the whole code,with db connection.i want to test in my localhost also.Kindly
Well, my answer solve the error you describe in your question. But it seems that you have others errors now. You have 2 options: update this question with more information about new errors (and the code that provide these errors) or post a new question with new errors.
1

Don't refer to team[]. Instead use

if (isset($_POST['team'])) {
    foreach($_POST['team'] as $value) { ... }
}

3 Comments

Invalid argument supplied for foreach()
@FAIQNASEEM is $_POST['team'] set?
@FAIQNASEEM do a var_dump() on $_POST and see what it contains when you submit the form.
1

When you will receive from the $_POST the variable team, it will be an array of every checkbox.

So instead of using team[] you should use

foreach($_POST['team'] as $value)

Also, you should be aware that if no checkbox were checked, the key team will not exist, so make sure before your foreach to do a if (isset($_POST['team']))

also you are overwriting insert at every time you loop.

you may want to do something like this :

if(isset($_POST['submit']))
{
    if (isset($_POST['team']))
    {
        $insert = true;

        foreach($_POST['team'] as $value)
        {
            // we add security with mysql_real_escape_string
            $value = mysql_real_escape_string($value);
            $result = mysql_query("INSERT INTO team('team') VALUES ('$value')");

            if (!$result)
            {
                $insert = false;
                break;
            }
        }

        if($insert)
        {
            echo "dONE";
        }
    }
}

you may want to check mysql_real_escape_string to add secrurity to your code

1 Comment

@FAIQNASEEM Depending of what data you want to store into your database, yes this is enough.
0

when you do name=something[] on an input, php will create the array for you so you just need to do

foreach($_POST['team'] as $value)

Also something like name=team[member][] could be iterated through in php like so

foreach($_POST['team']['member'] as $value)

So it makes grouping inputs and well as processing multiple dynamic inputs very easy

1 Comment

Invalid argument supplied for foreach()
0

Try the following:

$teams = $_POST['team'];
$submitted = $_POST['submit'];

if(isset($submitted) && is_array($teams)) {

     foreach($teams as $team) {
          /* Escapes special characters in a string for use in an SQL statement */ 
          $team = mysql_real_escape_string($team)
          $insert = mysql_query("INSERT INTO team('team') VALUES ('$team')");
     }

     if($insert) {
          echo "dONE";
    }
} else {
    echo "Either Form Not Submitted or Teams is Not Array";
}

4 Comments

Invalid argument supplied for foreach()
If you are still getting "Invalid argument supplied foreach()", try my revised answer.
Either Form Not Submitted or Teams is Not Array,this is printed on page
right before the if statement, try print_r($_POST['team']) does that show you the values you checked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.