如果复选框更新值,如果选中多行php

时间:2017-04-08 05:39:59

标签: php html sql

如果选中该复选框,我尝试更新值为1的每行,否则不执行任何操作。

enter image description here

$employees = Employee::get():

代码(它不起作用)

$query= "SELECT Name, Surname, Age, Club, age_group, School, team_select FROM players WHERE Age < 9";
$statement = $db->prepare($query);
$statement->execute();
$players = $statement->fetchAll(PDO::FETCH_ASSOC);
echo '<form nethod="POST" action="add_to_team.php">';
echo '<p align="center"><a href="new.php" >Add Player</a></p>';
echo '<table class="table table-bordered"';
echo '<tr><th>Name</th>
      <th>Surname</th>
      <th>Club</th>
      <th>Age Group</th>
      <th>School</th>
      <th>Edit</th>
      <th>Delete</th>
      <th>Add to Team</th></tr>';


// loop through results of database query, displaying them in the table
 foreach ($players as $player) {
// echo out the contents of each row into a table
echo '<tr>';
echo '<td>' . $player['Name'] . '</td>';
echo '<td>' . $player['Surname'] . '</td>';
echo '<td>' . $player['Club'] . '</td>';
echo '<td>' . $player['age_group'] . '</td>';
echo '<td>' . $player['School'] . '</td>';
echo '<td><a href="edit.php?id=' . $player['Name'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $player['Name'] . '">Delete</a></td>';
echo '<td><input type="checkbox" name="team_select" value="1"> </td>';


echo '</tr>';

}
echo '</table>';
echo'<input type="submit" value="Submit" name="submit"><br/>';

1 个答案:

答案 0 :(得分:2)

您需要为复选框指定不同的名称。如果它们都被命名为team_select,则只会有一个$_POST['team_select'],但您无法确定选中了哪个复选框。

使用name="team_select[]"并将它们全部放入数组中。然后你可以将玩家名称放入value,这样数组就会包含应该添加的所有玩家的名字。

echo '<td><input type="checkbox" name="team_select[]" value="' . $player['Name'] . '"> </td>';

当您处理表单时,您可以执行以下操作:

foreach ($_POST['team_select'] as $name) {
    ...
}

处理所有被选中的玩家。

相关问题