简体   繁体   English

用相同的值更新多行PHP

[英]Update Multi Row PHP with same value

I retrieve multiple rows from one table and I display them in a table like image in -> Click here for view the image <- 我从一张表中检索多行,并将它们显示在类似图像的表中-> 单击此处查看图像 <-

In the database, "status" value is set as "pending" so I want to choose several student and change the "status" to "approved" and then make one update that updates every row that i checked. 在数据库中,“状态”值设置为“待处理”,因此我想选择几个学生并将“状态”更改为“已批准”,然后进行一次更新,以更新我检查的每一行。

This is my code for the image above... 这是我上面图片的代码...

        <?php
        include("connection.php");


        echo "<table border='1'><tr>

        <td><strong>Student ID</strong></td>
        <td><strong>Student Name</strong></td>
        <td><strong>Kelompok</strong></td>
        <td><strong>Block</strong></td>
        <td><strong>Level</strong></td>
        <td><strong>House</strong></td>
        <td><strong>Status</strong></td>


        </tr>";
        $i=0;
        while ($ww=mysqli_fetch_array($query))
        {
            if ($i%2==0)
                $class="evenRow";
            else
                $class="oddRow";

            $id=$ww[0];
            $studentid=$ww[1];
            $name=$ww[2];
            $kelompok=$ww[8];
            $block=$ww[9];
            $level=$ww[10];
            $house=$ww[11];
            $status=$ww[14];



        echo "<tr>
            <input type=hidden name=applyid[] value=".$id."/>
            <td>$studentid</td>
            <td>$name</td>
            <td>$kelompok</td>
            <td>$block</a></td>
            <td>$level</td>
            <td>$house</td>
            <td>
                <input type=checkbox name=status[] value=".$id."approved checked> APPROVED <br>
            </td>
            </tr>"; 
        }
        $i++;
        echo "</table>";

        ?>

    <br>

    <a href="officerapplicationupdate.php?applyID=<?php echo $id; ?>"><input type="submit" value="Update"></a>
    </form>

    <br><br>  
    </table>

and this is the code for updating the row.. 这是用于更新行的代码。

<?php

    include("connection.php");



    if(isset($_POST["submit"]) && $_POST["submit"]!="") {
        $idCount = count($_POST["status"]);
        for($i=0;$i<$idCount;$i++) {
            mysqli_query("UPDATE application SET apply_status='".$_POST["status"][$i]."'
            WHERE apply_id='".$_POST["applyid"]."'");
        }
    }

?> 

You can prepare your statement and execute it afterwards. 您可以准备您的语句,然后执行它。 This can be done like this: 可以这样完成:

   $in = array();
    if(isset($_POST["submit"]) && $_POST["submit"]!="") {
        $idCount = count($_POST["status"]);
        for($i=0;$i<$idCount;$i++) {
         $in[] .= $_POST["applyid"][$i];
        }
    }
    mysqli_query("UPDATE application SET apply_status='approved' WHERE apply_id IN '".implode(",",$in)."'");

So only one query is needed. 因此只需要一个查询。

You can also sum up the changes for other apply_status. 您还可以总结其他apply_status的更改。

Be aware to check the input comming from webpage to prevent SQL injection! 注意检查来自网页的输入以防止SQL注入!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM