Php 如何在foreach循环中使用公共索引键传递多个值?

Php 如何在foreach循环中使用公共索引键传递多个值?,php,mysql,loops,foreach,Php,Mysql,Loops,Foreach,我需要在foreach循环中使用一个公共索引键传递两个或多个输入,以便一次更新多个mysql行 前端: <table> <thead> <tr> <th>ID</th> <th>Column 1</th> <th>Column 2&

我需要在foreach循环中使用一个公共索引键传递两个或多个输入,以便一次更新多个mysql行

前端

<table>
                <thead>
                <tr>
                  <th>ID</th>
                  <th>Column 1</th>
                  <th>Column 2</th>
                </tr>
                </thead>

                <tbody>

        <form method="post" action="process.php">

        <?php
        $stmt = $mysqli->prepare("SELECT id,column1,column2 FROM table");
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($id,$column1,$column2);
        while ($stmt->fetch()) {

            ?>
                <tr>
                  <td><?php echo $id ?></td>

                  <!-- Here User will input values for the below two fields in all the rows -->

                  <td><input type="text" name="column1[<?php echo $id; ?>]"/></td>
                  <td><input type="text" name="column2[<?php echo $id; ?>]"/></td>


                </tr>
        <?php } ?>
                <input type="submit" name="submit" value="Update all">
                </form>
                </tbody>

</table>
<?php
if(isset($_POST['submit'])){

        foreach($_POST['column1'],$_POST['column2'] as $key=>$value,$value1){ //column1 and column2 have common $id in each row

            $stmt = $mysqli->prepare("UPDATE table SET column1 = ?, column2 = ? WHERE id = ?");
            $stmt->bind_param('ssi',$value,$value1,$key);
            $stmt->execute();

        }

        if ($stmt->execute()) {
            echo "Done!";
            exit();
        }

}

?>

身份证件
第1栏
第2栏

您应该在循环之前进行
准备
绑定参数
,而不是每次。@Barmar Ok注意到了!谢谢,您应该在循环之前进行
准备
绑定参数
,而不是每次。@Barmar Ok注意到了!非常感谢。
<?php
foreach (array_keys($_POST['column1']) as $key) {
  $value = $_POST['column1'][$key];
  $value1 = $_POST['column2'][$key];
  $stmt = $mysqli->prepare("UPDATE table SET column1 = ?, column2 = ? WHERE id = ?");
  $stmt->bind_param('ssi',$value,$value1,$key);
  $stmt->execute();
}