在PHP表单中选择了错误的变量,添加错误的人

I've managed to make my "Add a member" function work. Not the way I wanted it to though. This script shows every member not in the selected team in a list, with an "Add to team" button right next to it. When that button is clicked, the person next to the button should be added to the team..

..however, that's not how it works. It doesn't add the person right next to the button, it adds someone else on the list to the team. It seems to add the very last person on the list to a team for some reason?

The table list shows up something like this:

John Doe   [ADD]
John Smith [ADD]
John Mo    [ADD]

But whoever I click when the list looks like this, first John Mo will be added, and then John Smith.

well i sure does not work the way you want,i prefer to use select box or radio button or check box and only one submit button for the whole form.the problem is the form send two values on submit,teamid,userid. and because you have multiple filelds name teamid and userid form sends the last one's value.i changed your code as below to generate forms for each user:

  echo "<p class='ca'>Team $teamname</p>";
        echo "<table>";

        //The add a member form starts here
        while ($rowa = mysqli_fetch_array($resulta)) {
            $useridb = $rowa['userid'];
            $fnamea = $rowa['fname'];
            $lnamea = $rowa['lname'];
            echo "<tr>";

        echo "<form>";
            echo "<td>";
            echo $fnamea.' '.$lnamea;
            echo "</td>";
            echo "<td>";
            echo "<input type='hidden' name='teamid' value='$teamlol'>";
            echo "<input type='hidden' name='userid' value='$useridb'>";
            echo "<button name='addmember' class='mybutton'><img src='add.png' style='width:15px;height:15px;'></button>";
            echo "</td>";

        echo "</form>";
            echo "</tr>";
        }

        echo "</table>";

on at last use this code to save data:

      if(isset($_GET['addmember'])) {
        $sqlz = "INSERT INTO userteam (userid, teamid) VALUES ('".$_GET['userid']."','".$_GET['teamid']."')";
        $resultz=$mysqli->query($sqlz);

        $sqly = "INSERT INTO result (userid, teamid) VALUES ('".$_GET['userid']."','".$_GET['teamid']."')";
        $resultz=$mysqli->query($sqly);

    }