Delete Multiple Rows by Selecting Checkboxes Using PHP

Delete multiple rows by selecting checkboxes using PHP

You should treat it as an array like this,

<input name="checkbox[]" type="checkbox" value="<?php echo $row['link_id']; ?>">

Then only, you can take its count and loop it for deletion.

You also need to pass the database connection to the query.

$result = mysqli_query($dbc, $sql);

Yours did not include it:

$result = mysqli_query($sql);

PHP - Delete multiple rows via Checkbox

You can simply use one query to delete all records without the pain of for loop

 include 'config.php';
$array = $_POST['checkbox'];
$listCheck = "'" . implode("','", $array) . "'";
echo $sql6 = "DELETE FROM `customer` WHERE `name` IN ($listCheck)";
$delete = mysqli_query ($conn,$sql6);

Also there is issue in this code. Replace the below line with your code

echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='" . $row['name'] . "'></td>";

Deleting multiple rows using checkboxes, PHP and MySQL

in html page do it like this

<input name="checkbox[<?php echo $row['id']?>]"

and in the back end do like this

foreach ($delete as $id => $val) {
if($val=='checked'){
$query="DELETE FROM documents WHERE id = '".$id."'";
$result= mysqli_query($con, $query) or die("Invalid query");
}
}

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

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.

Deleting multiple rows using checkbox

Need to first set form method to `POST'

So, modify

<form action="inc/delete.php" method="GET">

To:

<form action="inc/delete.php" method="post">

And in inc/delete.php,

if (isset($_POST['submit'])){ // Check if form is submitted.
if (! empty($_POST['checkbox'])){ // Check if Checkbox is checked.
// Loop over the checkbox and get selected ids.
foreach($_POST['checkbox'] as $checkbox_id){
echo $checkbox_id."<br/>";
// Add your delete query here.
}
}
}

Deleting multiple rows from database by checkboxes

Javascript

Since you are using jquery there is better way :)

<script type="text/javascript">
var is_activate = true; // we will track which input button was clicked :)

jQuery(function($) {
$("#form input#check_all").change(function() {
var inputs = $("#form input[type='checkbox']");
if ( $(this).is(":checked") ) {
inputs.prop( "checked", true );
// inputs.attr( "checked", true ); // if its not working
}
else {
inputs.removeAttr( "checked" );
}
});

// Track clicked button
$("#form input[type=submit]").on("click",function(e) {
is_activate = ( $(this).hasClass("activate_btn") ) ? true : false;
});

$("#form").submit(function(e) {
e.preventDefault();
var string = ( is_activate ) ? 'activate' : 'delete';
var data = $(this).serialize();
var checked = $(this).find("input[name='data[]']:checked").length;
if ( checked == 0 ) {
alert( "Please select a comment(s) to "+string+"." );
return false;
}
var text = "Are you sure you want to "+string+" these comment"+( ( checked == 1 ) ? "?" : "s?" );
if ( confirm( text ) ) {
$.ajax({
url: 'resources/ajax/'+( ( is_activate ) ? 'ajax_activate_comment.php' : 'ajax_delete_comment.php' ),
type: 'post',
data: data,
success: function( data ) {

}
});
}
});
});
</script>

HTML

<form method="post" id="form">
<label>Check All</label>
<input type="checkbox" id="check_all" value="">

<label>Here im displaying record from database and</label>
<input name="data[]" type="checkbox" id="data1" value="1">
<input name="data[]" type="checkbox" id="data2" value="2">

<!-- Activate Button -->
<input class="activate_btn" type="submit" name="activate" value="Activate" id="submit">
<!-- Delete Button -->
<input class="delete_btn" type="submit" name="delete" value="Delete" id="submit">
</form>

PHP

A single query is enough :)

<?php
if ( isset( $_POST['data'] ) ) {
$id_array = $_POST['data'];
if ( !empty( $id_array ) ) {
$id_array = implode( ",", $_POST['data'] ); // dont forget to sanitize
$sql = $db->query( "DELETE FROM comments WHERE `id` IN (".$id_array.")" );
}
}
?>

And remember, its not good that doing this all in client side.

You can do POST request to a single file, since your each input button has a unique name.

So in your PHP code, you can find which button was clicked like this.

<?php
if ( isset( $_POST["activate"] ) ) {
$sql = $db->query( "UPDATE comments SET status = '1' WHERE `id` IN (".$id_array.")" );
}
else {
$sql = $db->query( "DELETE FROM comments WHERE `id` IN (".$id_array.")" );
}
?>

look how simple :) Isn't it?



Related Topics



Leave a reply



Submit