0

I'm trying to add a check box to each row of my search results to allow the selected row of data to be deleted from MySQL and I'm stuck on how to do it.

here is what I have for my code I want to add the delete feature to.

 echo '<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">';
    echo '<th>ID</th><th>Team</th><th>Last Name</th><th>First Name</th><th>Registered</th><th>Payment</th><th>Physical</th><th>Photo Taken</th><th>Member Photo</th><th>View Member</th><th>Edit Member</th><th>Delete</th>';
    while($rows = mysql_fetch_array($raw_results)){
    // If $color==1 table row color = #FFC600
    if($color==1){
    echo "<tr bgcolor='#C6E7F7'>
    <td><center>".$rows['id']."</td></center><td><center><img src='".$rows['logo_src']."'height='30' width='50'/></td><td><center>".$rows['last_name']."</td></center><td><center>".$rows['first_name']."</center></td><td><center>".$rows['registration']."</center></td><td><center>".$rows['pay_status']."</center></td><td><center>".$rows['physical']."</center></td><td><center>".$rows['photo']."</center></td><td><center><img src='".$rows['member_photo']."'height='30' width='50'/></center></td><td><center><a href=view_member.php?id=$rows[id]><img src=data/icons/viewmore.gif height='20' width='20' border='0'/></center></td><td><center><a href=edit_member.php?id=$rows[id]><img src=data/icons/viewmore.gif height='20' width='20' border='0'/></center></td><td><center></tr>";

so the very last column where it say's delete, I want to add a checkbox in the rows below that and then a button below the search results box to delete the selected row. I'm stuck on how to add the checkbox.

Here's a pic of the results page

enter image description here

user2447848
  • 289
  • 5
  • 17
  • possible duplicate of [Deleting multiple rows using checkboxes, PHP and MySQL](http://stackoverflow.com/questions/10145717/deleting-multiple-rows-using-checkboxes-php-and-mysql) – Stefan Jul 16 '13 at 09:28
  • Stop it, both of you. – BoltClock Jul 17 '13 at 06:51

5 Answers5

3

Wrap the whole table and button in a form:

<form action="" method="post">
...
...
</form>

Inside your while loop, add a checkbox to the delete cell:

// concatenate or echo as required
<input type="checkbox" name="delete[]" value="<?php echo (int)$rows['id']; ?>" />

Make sure your button is of type submit:

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

On the PHP side:

if(isset($_POST['delete']) && is_array($_POST['delete']) && count($_POST['delete']) > 0){
    foreach($_POST['delete'] as $deleteId){
        // run query to delete $deleteId
    }
}

This works because PHP parses the checkboxs with the names delete[] into a native array

MrCode
  • 63,975
  • 10
  • 90
  • 112
1

Use a checkbox array :) Set that input field for every row in your table :

<input type="checkbox" name="delete_list[]" value="{ID OF THE ENTRY}">

You'll receive an array in $_POST['delete_list'], which you'll be able to browse in order to see what checkboxes were checked :)

Still, already answered :

Community
  • 1
  • 1
John WH Smith
  • 2,743
  • 1
  • 21
  • 31
1
 echo '<table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">';
echo '<th>ID</th><th>Team</th><th>Last Name</th><th>First Name</th><th>Registered</th><th>Payment</th><th>Physical</th><th>Photo Taken</th><th>Member Photo</th><th>View Member</th><th>Edit Member</th><th>Delete</th>';
while($rows = mysql_fetch_array($raw_results)){
// If $color==1 table row color = #FFC600
if($color==1){
echo "
 <tr bgcolor='#C6E7F7'>
        <td><center>".$rows['id']."</center></td>
        <td><center><img src='".$rows['logo_src']."'height='30' width='50'/></td>
        <td><center>".$rows['last_name']."</td></center>
        <td><center>".$rows['first_name']."</center></td>
        <td><center>".$rows['registration']."</center></td>
        <td><center>".$rows['pay_status']."</center></td>
        <td><center>".$rows['physical']."</center></td>
        <td><center>".$rows['photo']."</center></td>
        <td><center><img src='".$rows['member_photo']."'height='30' width='50'/></center></td>
        <td><center><a href=view_member.php?id=$rows[id]><img src=data/icons/viewmore.gif height='20' width='20' border='0'/></center></td>
        <td><center><a href=edit_member.php?id=$rows[id]><img src=data/icons/viewmore.gif height='20' width='20' border='0'/></center></td>
        <td><center><input type='checkbox' name='delete[]' value='".$rows['id']."'></center></tr>";

You will get the results in an array which you will use in your query to delete the rows from mysql

Kirtan
  • 120
  • 1
  • 6
0

You could add a col with this input:

<input type="checkbox" name="delete[]" value="<?=$row['id]'?>" />

Please check your HTML Code. It is not valid...

Michael Walter
  • 1,427
  • 12
  • 28
0

this is code and does not get array.

if(isset($_POST['submit']))
{

$check=array($_POST['checkbox']);
//$id_array = array($check);
 echo "<pre>";
echo print_r($check);
exit; 
$id_count = count($_POST['checkbox']);

for($i=0; $i < $id_count; $i++) {
    $id = $check[$i];

  //  if ($id > 0) 
    //{

            $res =mysql_query( "delete from employee WHERE  id ='$id'");
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105