简体   繁体   中英

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.

Be aware to check the input comming from webpage to prevent SQL injection!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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