Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-04-2008, 10:38 AM
Junior Member
 
Join Date: Sep 2008
Posts: 1
Default Multiple Row Deletions

How to delete multiple rows from DB, by selecting checkboxes from the list and click on delete button.

Also if no checkbox is selected, then it should fire an error like "Select atleast one checkbox.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-04-2008, 02:41 PM
bpat1434's Avatar
Member
 
Join Date: Jul 2008
Posts: 34
Default

Well, I'd create a group of checkboxes. Make the name of the checkbox "remove[{$id}]", give it a value of $id, and that's it for the form. So for example...
PHP Code:
$result $stmt->fetchAll();

foreach(
$result as $row)
{
    
$this->addEelemnt('checkbox''remove['.$row['id'].']', array(
        
'value' => $row['id'],
        
'label' => $row['title'],
    );

Now, once they submit the form, all you have to do is look at $_POST['remove'] and you have all the IDs they want to delete. From there you can either run multiple delete() calls on the database, or do something like:
PHP Code:
$db->delete('table''id IN(' implode(','$_POST['remove']) . ')'); 
Hope that helps.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-06-2008, 06:26 AM
Junior Member
 
Join Date: Oct 2008
Posts: 2
Default

This code have the SQL-injection:
PHP Code:
$db->delete('table''id IN(' implode(','$_POST['remove']) . ')'); 
Better use quote() method:
PHP Code:
$db->delete('table''id IN(' $db->quote($_POST['remove']) . ')'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-06-2008, 11:19 AM
bpat1434's Avatar
Member
 
Join Date: Jul 2008
Posts: 34
Default

It was just demo code, I would expect the OP to be able to realize that of course there should be some sanitation going on prior to that.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:56 PM.