Skip to content

How to delete multiple rows from mysql database with checkbox using PHP?

I try to delete my data in “admin” database, but the delete button does not function.

This is my top part

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="admin"; // Database name 
$tbl_name="admin"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";

This is my checkbox code

<td><?php echo $rows['course_code']; ?></td>
<td><?php echo $rows['course_name']; ?></td>
<td><?php echo $rows['lecture_id']; ?></td>
<td><input name="checkbox[]" type="checkbox"
    id="checkbox[]" value="<?php echo $rows['course_code'];?>"></td>

and, this is my button code

<input type='button' id="delete" value='Delete' name='delete'>

This is my php function code

$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
echo "<meta http-equiv="refresh" content="0;URL=delete.php">";



include all the input elements within your <form> tags: <form> all inputs are here </form>


<input name = "checkbox[]" type="checkbox"  id="checkbox[]" value="<?php echo     $rows['course_code'];?>">

to (id doesn’t matter here):

<input name="checkbox[]" type="checkbox"  value="<?php echo $rows['course_code'];?>"/>

and your button code:

<input type='button' id="delete" value='Delete' name='delete'>


<input type="submit" value="Delete"/>

set opening <form> tag to <form action="delete.php" method="post">

Note: I assume below codes are in delete.php file. if not replace “delete.php” with that name in above opening form tag.

your delete.php file:

$cheks = implode("','", $_POST['checkbox']);
$sql = "delete from $tbl_name where course_code in ('$cheks')";
$result = mysql_query($sql) or die(mysql_error());

Note: Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in “Loaded Configuration File” row on that page. You can see php info page by loading below php file in the browser:


open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there. also search for “extension_dir” and open the directory it says and make sure php_mysqli.dll file is there. (you may have .so extension if you not use windows OS)

Then restart your server and you are done!

By Fred -ii-

Using mysqli_ with prepared statements is indeed a better and safer method. However, some will even suggest PDO, but even PDO doesn’t have some of the functionalities that mysqli_ offers; strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false. -Thanks Fred.