-2

I want to select multiple items to delete the records in the database. But i am not being able to. My delete.php is fine. How can i use it to delete multiple selected items. I just created a checkbox. How can i make that checkbox work.

echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
while ($myrow = $result->fetch_assoc())
{
    echo "<tr><td><input type='checkbox'name='mycheck'onclick='toggleform('document.myform','mycheck','ptext')'></td>";
    echo "<TD>".$myrow['id']."</TD>";
    echo "<TD>".$myrow['name']." </TD>";
    echo "<TD><a href=\"view1.php?id=".$myrow['id']."\">View</a> ";
    echo "<a href=\"delete.php?id=".$myrow['id']."\">Delete</a> ";
    echo "<a href=\"edit1.php?id=".$myrow['id']."\">Edit</a>";
}
<input type="button" name= "ptext" value="Delete selected">
micky
  • 277
  • 1
  • 13
  • 39
  • Possible duplicate of [Delete multiple rows by selecting checkboxes using PHP](http://stackoverflow.com/questions/14475096/delete-multiple-rows-by-selecting-checkboxes-using-php) – Atilla Arda Açıkgöz Feb 05 '16 at 08:52

2 Answers2

1

You have to create checkbox name as array type i.e. mycheck[]. Then, In delete_all.php page, find total checked checkbox and delete it accordingly.

<form method='POST' action="delete_all.php">

    <?php
    echo"<TR><TD>Select</td><TD>S.N.</td><td><B>Full Name</B></td><td><B>Options</B></td></TR>";
    while ($myrow = $result->fetch_assoc())
    {
          echo "<tr><td><input type='checkbox' name='mycheck[]' value=".$myrow['id']."></td>";
          echo "<TD>".$myrow['id']."</TD>";
          echo "<TD>".$myrow['name']." </TD>";
          echo "<TD><a href=\"view1.php?id=".$myrow['id']."\">View</a> ";
          echo "<a href=\"delete.php?id=".$myrow['id']."\">Delete</a> ";
          echo "<a href=\"edit1.php?id=".$myrow['id']."\">Edit</a>";
    }
    ?>
    <input type="submit" name= "ptext" value="Delete selected">

</form>

delete_all.php

<?
extract($_POST);

$totalCheckboxChecked = sizeof($mycheck);

for($i=0;$i<$totalCheckboxChecked;$i++)
{
    $idToDelete = $mycheck[$i];

    echo "DELETE FROM table_Name WHERE id_ColumnName = '$idToDelete'";

    // Execute Your Querys
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

Are you getting all ids on the delete.php page? if yes then simple use

DELETE FROM tableName
WHERE id IN ($id1 , $id2 , $id3 , etc);

if not, The way i would do it is, i would use form, and then post it in the delete.php page using method post, and then simply take the values from super global

$_POST[]

and use the above query, hope this helps.

Talha Abrar
  • 880
  • 8
  • 22