deleting multiple records with check box selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

deleting multiple records with check box selection

Post by nite4000 »

Hey guys thanks for the help on my last posts. I have one other thing I am stuck on.

I make a email system and I have the selectall working to select all emails which check all the checkboxes now I need to either make a page with the code on it or java or something to when i click delete it will remove them.

I am so close the samples i have found just isnt working like i want. I know arrays are involved but just not getting it correct.

Here is my code if it will help

Code: Select all

 
<table width="90%" align="center">
<tr><td bgcolor="#33CCFF"><input type="checkbox" name="checkall2" onclick="checkUncheckAll(this);"/>
Select All | [b]<input name="delete" type="submit" value="Delete">[/b]</td>
</tr></table>
 
<table width="90%" align="center">
<tr>
  <td width="17%">
<tr>
            <td align="left"><input type="checkbox" name="checkall" onclick="checkUncheckAll(this);"/>Select All | </td>
            <td></td>
            <td width="23%" align="left">Email</td>
            <td width="33%">Subject</td>
            <td width="18%">Date Received</td><td width="9%"></td>
  </tr>
            [b]<?php
                        
                 $r = mysql_query("SELECT * from blah where email='" . $_SESSION['sess_name'] . "' and status='1' OR email='admin@blah.com' and status='4'  and id= '$usrid' ORDER BY SENT DESC ");
            while($info = mysql_fetch_array($r, MYSQL_ASSOC)) {
echo'<tr><td><input type="checkbox" name="'.$info['id'].'" id="'. $info['id'].'" value="'.$info['id'].'">
</td><td><img src="../images/email.png"></td>
              <td align="left"><a href="surf_mail.php?id=' . $info['id'] . '">admin@blah.com</a></td>
              <td align="left">'.$info['subject'].'</td>
              <td>'.date("n-d-y",strtotime($info['sent'])).'</td><td><a href="mailbox.php?id=' . $info['id'] . '" onclick="JavaScript&#058;return(confirm_delete('.$info['id'].'))">Delete</a></td>
            </tr>';
            }
                
     
        ?>[/b]</table></form>
 
 
sung
Forum Newbie
Posts: 1
Joined: Sat Jun 27, 2009 10:51 am

Re: deleting multiple records with check box selection

Post by sung »

Do u want to remove the record from database or only from page.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: deleting multiple records with check box selection

Post by McInfo »

A quick, incomplete example

form.php

Code: Select all

<?php
$rows = array
(   array('id' => '520', 'name' => 'Pilot')
,   array('id' => '624', 'name' => 'Poker')
,   array('id' => '302', 'name' => 'Poncho')
,   array('id' => '415', 'name' => 'Pika')
,   array('id' => 'bad', 'name' => 'Bad Input')
);
?>
<form method="post" action="delete.php">
    <table>
        <tbody>
<?php foreach ($rows as $row) : ?>
            <tr>
                <td><input id="chk<?php echo $row['id']; ?>"
                           type="checkbox"
                           name="delete[]"
                           value="<?php echo $row['id']; ?>" /></td>
                <td><label for="chk<?php echo $row['id']; ?>"><?php echo $row['name']; ?></label></td>
            </tr>
<?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="2"><input type="submit" value="Delete Selected" /></td>
            </tr>
        </tfoot>
    </table>
</form>
delete.php

Code: Select all

<?php
echo '<pre>', print_r($_POST, 1), '</pre>';
if (isset($_POST['delete']) && is_array($_POST['delete'])) {
    $values = array();
    foreach ($_POST['delete'] as $row_id) {
        if (0 == preg_match('/[^0-9]/', $row_id)) {
            $values[] = $row_id;
        }
    }
    if (count($values) > 0) {
        $query = 'DELETE FROM `table` WHERE `id` IN ('.implode(', ', $values).')';
        echo "<pre>$query</pre>";
    }
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 12:45 pm, edited 1 time in total.
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: deleting multiple records with check box selection

Post by nite4000 »

I was looking for a sample with teh code i have already I am afraid i am lost on this other code. Is tehre anyone else that can help me out
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: deleting multiple records with check box selection

Post by McInfo »

The point of my script is that each of the checkboxes has the name="delete[]" attribute. When the form is submitted to PHP, this creates an array like

$_POST

Code: Select all

Array
(
    [delete] => Array
        (
            [0] => 520
            [1] => 624
            [2] => 302
        )
)
The array contains the primary keys of the rows to be deleted. By imploding the array, a query can be created that will delete the three rows with the given IDs.

Code: Select all

DELETE FROM `table` WHERE `id` IN (520, 624, 302)
Edit: This post was recovered from search engine cache.
Post Reply