PHP Code Delete Multiple Rows - Php

How to delete multiple rows using PHP code?

Snippet Code

Rate this page :
  [ 0 votes]

The below sample code helps you to delete multiple rows by selecting the checkbox.

<?php error_reporting(0); $link = mysql_connect("localhost", "root", ""); $dbc=mysql_select_db('testdb',$link); $query = "SELECT * FROM usertable"; $result = mysql_query($query) or die('Error querying database'); $count=mysql_num_rows($result); ?> <form name="form1" method="post" action=""> <table border="1"> <caption><b>Delete Multiple Rows</b></caption> <?php while ($row=mysql_fetch_array($result)) { ?> <tr> <td align="center"><input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>"></td> <td><?php echo $row['id']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['city']; ?></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input name="delete" type="submit" value="Delete"></td> </tr> <?php if(isset($_POST['delete'])) { $name = $_POST['checkbox']; foreach ($name as $checkbox){ $del_id = $checkbox; $sqlquery = "DELETE FROM usertable WHERE id='$del_id'"; $result = mysql_query($sqlquery); } if($result){echo "Deleted";} } mysql_close(); ?> </table> </form>


Ask Questions

Ask Question