Delete and Update Multiple Rows Using PHP
Delete and Update Multiple Rows Using PHP
In this tutorial, we will select multiple rows through checkboxes and we will update/delete them.
First, you will have to make a database:
Index.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<body><center> <div style="width:500px;"> <form name="frmUser" method="post"> <table border="0" cellpadding="20" cellspacing="2" width="500" class="tblListForm"> <tr class="head"> <td></td> <td><h2>UserName</h2></td> <td><h2>First Name</h2></td> <td><h2>Last Name</h2></td> </tr> <?php $i=0; while($row = mysqli_fetch_array($result)) { ?> <tr class="rows"> <td><input type="checkbox" name="members[]" value="<?php echo $row["ID"]; ?>" ></td> <td><?php echo $row["Username"]; ?></td> <td><?php echo $row["FirstName"]; ?></td> <td><?php echo $row["lastName"]; ?></td> </tr> <?php $i++; } ?> <tr class="head"> <td colspan="4"><input id="update" type="button" name="Edit" value="Edit" onClick="setUpdateAction();" /> <input id="del" type="button" name="delete" value="Delete" onClick="setDeleteAction();" /> </td> </tr> </table> </form> </div></center> </body> |
Update.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<body> <form name="frmUser" method="post" action=""> <center> <div style="width:500px;"> <table border="0" cellpadding="10" cellspacing="0" width="500" align="center"> <tr class="head"> <td>Edit Member</td> </tr> <?php $count = count($_POST["members"]); for($i=0;$i<$count;$i++) { $result = mysqli_query($conn, "SELECT * FROM members WHERE ID='" . $_POST["members"][$i] . "'"); $row[$i]
= mysqli_fetch_array($result); ?> <tr> <td> <table border="0" cellpadding="20" cellspacing="0" width="500" align="center" class="tblSaveForm"> <tr> <td><label>Username</label></td> <td><input type="hidden" name="ID[]" class="txtField" value="<?php echo $row[$i]['ID']; ?>"><input type="text" name="Username[]" class="txtField" value="<?php echo $row[$i]['Username']; ?>"></td> </tr> <tr> <td><label>Password</label></td> <td><input type="password" name="password[]" class="txtField" value="<?php echo $row[$i]['password']; ?>"></td> </tr> <td><label>First Name</label></td> <td><input type="text" name="FirstName[]" class="txtField" value="<?php echo $row[$i]['FirstName']; ?>"></td> </tr> <td><label>Last Name</label></td> <td><input type="text" name="lastName[]" class="txtField" value="<?php echo $row[$i]['lastName']; ?>"></td> </tr> </table> </td> </tr> <?php } ?> <tr> <td colspan="2"><input type="submit" name="submit" value="Submit" class="btnSubmit"></td> </tr> </table> </div></center> </form> </body> |
Output:
Delete.php
1 2 3 4 5 6 7 8 |
<?php require_once "conn.php"; $Count = count($_POST["members"]); for($i=0;$i<$Count;$i++) { mysqli_query($conn, "DELETE FROM members WHERE ID='" . $_POST["members"][$i] . "'"); } header("Location:index.php"); ?> |
CSS:
You can download the CSS code from the download link below.
Download Code – delmultiplerows