Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何一次更新92行中的2列_Php_Mysql_Loops_For Loop - Fatal编程技术网

Php 如何一次更新92行中的2列

Php 如何一次更新92行中的2列,php,mysql,loops,for-loop,Php,Mysql,Loops,For Loop,我需要在带有前端HTML和后端PHP的MySQL表中使用唯一值更新92行中的2列。请注意,所有行都有唯一的ID。我认为,使用循环可以很容易地完成。但我不太熟悉循环,因为我对这个领域很陌生 这可能是一个重复的问题,但我没有从重复的问题中得到任何正确的答案。这就是我要发布的 这是我的前端部分: <table> <thead> <tr> <th>ID<

我需要在带有前端HTML和后端PHP的MySQL表中使用唯一值更新92行中的2列。请注意,所有行都有唯一的ID。我认为,使用循环可以很容易地完成。但我不太熟悉循环,因为我对这个领域很陌生

这可能是一个重复的问题,但我没有从重复的问题中得到任何正确的答案。这就是我要发布的

这是我的前端部分:

<table>
                <thead>
                <tr>
                  <th>ID</th>
                  <th>Column 2</th>
                  <th>Column 3</th>
                  <th>Column 4</th>
                </tr>
                </thead>

                <tbody>

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

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

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

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

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


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

    </table> 

身份证件
第2栏
第3栏
第4栏

考虑使用一个隐藏的数组输入字段,其值作为以后在HTML表单的
UPDATE
查询中所需的id。这样,用户不会看到或影响它。然后在提交时,遍历
$\u POST
数组的
计数

HTML(在while循环中)



所有92行是否都具有相同的id?哪些列获得随机值?@chris85否。。所有行都有唯一的ID。我没有回答你的第二个问题。随机值进入
column3
column4
?您的
其中id=?
将更新限制为一行。。。或者,您想在更新此行后更新所有其他行吗?@chris85用户将根据ID为这两列输入值。您的循环正在创建92个单独的表单。创建一个表单,并使用HTML数组将所有值传递给process.php,然后在process.php中从$\u POST数组中获取该值,并在每个查询中循环。
<?php
if(isset($_POST['submit'])){

        foreach($_POST['column3'] as $key=>$value AND $_POST['column4'] as $key=>$value1){

            $stmt = $mysqli->prepare("UPDATE records SET column3 = ?, column4 = ? WHERE id = ?");
            $stmt->bind_param('sss',$value,$value1,$key);
            $stmt->execute();

        }

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

}

?>
<td><input type="text" name="column3[]"></td>
<td><input type="text" name="column4[]"></td>
<input type="hidden" name="hiddenfield[]" value="<?php echo $id; ?>">
if(isset($_POST['submit'])){

  for ($i = 0; $i < count($_POST['hiddenfield']); $i++) {
     $key = $_POST['hiddenfield'][$i];
     $value1 = $_POST['column3'][$i];
     $value2 = $_POST['column4'][$i];

     $stmt = $mysqli->prepare("UPDATE records SET column3 = ?, column4 = ? WHERE id = ?");
     $stmt->bind_param('sss', $value1, $value2, $key);
     $stmt->execute();
  }

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