I have a form with multiple checkboxes in an array checkbox[]

And I am using the following code to try and insert values into my db

if (isset($POST['submit'])) {
foreach ($
POST['checkbox'] as $checkbox){
$cell=$checkbox[0];
$cell2=$checkbox[1];

$result = mysql_query("UPDATE *** SET match1='$cell' WHERE username='$username'");
$result = mysql_query("UPDATE *** SET match2='$cell2' WHERE username='$username'");

When I run the print_r($_POST['checkbox']) I get the following result

Array ( [0] => 1 [1] => 2 )

Which I thought would be ok, however the db is not updating as I would like, currently value of match1 is last checkbox and match2 does not have a value at all.

Can someone tell me where I have gone wrong.

Many thanks
Jon

    foreach($myArray as $var) {
    

    When you do that, you will loop through each element of the array $myArray. On each iteration, $var will contain the value of the current array element being looped on. In your code, if $_POST['checkbox'] is an array with two elements, the first having a value of 1 and the second a value of 2 (which is what your print_r() is indicating). Therefore, on the first iteration $checkbox will have a value of 1, and on the second iteration 2; so $checkbox will not be an array, but a single scalar value.

    As to what the correct code would be, I don't know enough about what you are trying to do, or what the form data looks like, to be able to answer that for you at this time.

      Hi NogDog thanks for your reply.

      Basically, what I am trying to create is a form that contains 60 matches and the username must select 10 of those matches.

      Therefore by creating an id to each checkbox am hoping to determine which 10 matches the user has selected.

        dr_overload wrote:

        When I run the print_r($_POST['checkbox']) I get the following result

        Array ( [0] => 1 [1] => 2 )

        Any ideas how I can store these values in my db:o

          Write a Reply...