FlyBoy
FlyBoy

Reputation: 11

How can I fetch and process PHP multiple $_POST records at once?

I'm designing a management interface where admins can modify members of a website all at once (bulk edit). We already have a single edit, but they want to be able to edit all the users at once and I wanted to do it so there is a single "Submit" button. The PHP code should then cycle through each record, look for changes and update as necessary. I know how to do everything but cycle through the records. I've tried creating an array, counting the posts records, I just haven't worked with arrays or mutli-dimensional arrays enough. Here's my existing code on the POST page:

<?php  $i = 0;
  while($member_list = mysql_fetch_array($getmembers)){  									 
  ?>
              <tr style="vertical-align:top;">
                    <td><input type="text" name="userid[<?=$i?>]" readonly value="<?php echo $id; ?>" style="width:40px;"></td>
                    <td><input type="text" name="username[<?=$i?>]" value="<?php echo $member_list['mem_username']; ?>" onchange='changes=true;' style="width:90px;"></td>
                    <td><input type="text" name="firstname[<?=$i?>]" value="<?php echo $member_list['first_name']; ?>" onchange='changes=true;' style="width:90px;"></td>
                    <td><input type="text" name="lastname[<?=$i?>]" value="<?php echo $member_list['last_name']; ?>" onchange='changes=true;' style="width:90px;"></td>
                    <td><input type="text" name="email[<?=$i?>]" value="<?php echo $member_list['email']; ?>" onchange='changes=true;'></td>
                    <td><textarea name="notes[<?=$i?>]" cols="20" rows="2" onchange='changes=true;'><?php echo $member_list['admin_notes']; ?></textarea></td>
              </tr>
<?php 	$i = $i + 1;	 } ?>

And it posts to:

$user_records = array(count($_POST['userid'])); // create the array

foreach ($user_records as $value) {  // go through array
    $user_records[] = array( // Go through the array
        $username => $_POST['username'.$i],
        $firstname => $_POST['firstname'.$i],
        $lastname => $_POST['lastname'.$i],
        $email => $_POST['email'.$i],
        $notes => $_POST['notes'.$i],
        );

        echo $username . ' | ' . $firstname; //this line is for testing to display only
        // Then posting comparison occurs and is updates as needed  
}

So, in a nutshell - when I run the above example, I get 5 pipes (the amount of seperators for the 6 fields) but don't get any data and it doesn't cycle through any of the other records. I know I'm missing a few things, just can't put my head in gear to see it. Any suggestions are greatly appreciated.

NOTE: obviously I'll prevent SQL injection and clean up things in the final version. I'm just trying to figure out the logic here.

Upvotes: 1

Views: 65

Answers (1)

Marc B
Marc B

Reputation: 360772

You never defined $i inside your processing loop, so you're doing accessing unknown/undefined array keys.

Since you've defined explicit keys in your html via the userid[$i] stuff, your loop should be:

foreach($_POST['userid'] as $key => $userid) {
    $username = $_POST["username"][$key];
    $firstname = $_POST["firstname"][$key]; 
    etc...

Note how each of your username/firstname/etc... have themselves become arrays. It's not username42, it's username[42] to access that particular field.

Upvotes: 2

Related Questions