Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Feb 2010
    Posts
    208
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Post Delete multiple rows from table

    Hi,

    I know how to delete one row from mysql table by using
    PHP Code:
    <input type="radio" name="ID" value="'.$IDE.'" id="e" >    
    and
    $delete mysql_query("DELETE FROM `my_options` WHERE id = '$IDE'"); 
    But now I need to delete multiple rows by one pushed button. Also, then I will need checkboxes to select multiple items...

    But how to delete selected??

    Thanks for your time.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    But how to delete selected??
    You could make a list of selected ids like
    (1,2,3,4,5 ...) and use that list, like
    Code:
    "DELETE FROM `my_options` WHERE id IN  (1,2,3,4,5)"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Feb 2010
    Posts
    208
    Thanks
    15
    Thanked 2 Times in 2 Posts
    this list is an array or what?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    no, it’s a string (like any value in a SQL string)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <form action="./" method="post">
    <input type="checkbox" name="foo[]" value="1" />
    <input type="checkbox" name="foo[]" value="2" />
    <input type="checkbox" name="foo[]" value="3" />
    <input type="checkbox" name="foo[]" value="4" />
    <input type="checkbox" name="foo[]" value="5" />
    <input type="submit" name="submit">
    </form>
    <?php
    if(isset($_POST['submit'])){
        if(isset(
    $_POST['foo'])){
            foreach( 
    $_POST['foo'] as $checkBoxValue ) {
                
    mysql_query("DELETE FROM `my_options` WHERE id = '" $checkBoxValue "'") or die ( mysql_error() );  
            }
        }
    }
    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    foreach( $_POST['foo'] as $checkBoxValue ) {
                
    mysql_query("DELETE FROM `my_options` WHERE id = '" $checkBoxValue "'") or die ( mysql_error() );  
            } 
    It's not a good practice to run query inside a loop, as it'll add overheads to the DB server.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    foreach( $_POST['foo'] as $checkBoxValue ) {
                
    $q mysql_query("DELETE FROM `my_options` WHERE id = '" $checkBoxValue "'") or die ( mysql_error() );
                
    mysql_free_result$q ); 
            } 
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php
    $str 
    "(" implode", "$_POST['foo']) . ")";  
    $q mysql_query("DELETE FROM `my_options` WHERE id IN " $str ) or die ( mysql_error() );
    mysql_free_result$q );        
    ?>
    Last edited by Phil Jackson; 04-03-2010 at 10:25 AM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #9
    Regular Coder
    Join Date
    Feb 2010
    Posts
    208
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Phil Jackson View Post
    PHP Code:
    <?php
    $str 
    "(" implode", "$_POST['foo']) . ")";  
    $q mysql_query("DELETE FROM `my_options` WHERE id IN " $str ) or die ( mysql_error() );
    mysql_free_result$q );        
    ?>
    you changed your script twice.... which is correct?

  • #10
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    both are, technically

    one is more resource efficient than the other as it doesn't make use of a query inside a loop, which as abduraooft stated, creates (unnecessary) overhead

  • Users who have thanked met for this post:

    auriaks (04-03-2010)

  • #11
    Regular Coder
    Join Date
    Feb 2010
    Posts
    208
    Thanks
    15
    Thanked 2 Times in 2 Posts
    thanks for answers

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    No sanitisation/validation of the id's has been done in those examples, however. Do the necessary checks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •