Skip to content
Advertisement

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

<?php
$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";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>

This is my checkbox code

<tbody>
<?php
    while($rows=mysql_fetch_array($result)){
?>
<tr>
<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>
<td><form>
</form>
</td>
</tr>
<?php
    }
?>
</tbody>

and, this is my button code

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

This is my php function code

<?php
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv="refresh" content="0;URL=delete.php">";
}
}
mysql_close();
?>

Advertisement

Answer

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

update:

<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'>

to

<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:

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

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:

<?php
 phpinfo();
?>

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.

Advertisement