Results 1 to 6 of 6

Thread: Multiple Delete Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple Delete Error

    Ok, I am attempting to make a script that will allow multiple deletion at the same time (via check boxes). I am not really sure how exactly to do it, but so far what I have picked up from a few tutorials is this:
    PHP Code:
    <?php
        
    require('config.php');
        
    $result mysql_query("SELECT * FROM `m_delete` ") or die (mysql_error());
    $count mysql_num_rows($result);

    echo 
    '<table width="400">';

    while(
    $row mysql_fetch_array($result)) {
        
    extract($row);

    echo 
    '
    <tr>
        <td>'
    .$id.'</td>
        <td><input type="checkbox" name="checkbox[]" /></td>
        <td>'
    .$name.'</td>
        <td>'
    .$email.'</td>
    </tr>
    '
    ;
    }

    echo 
    '</table>
        <br />
    <input type="submit" name="delete" id="delete" value="Delete Selected" />
    '
    ;

    if(
    $delete) {
        for(
    $i=0;$i<=$count;$i++) {
            
    $del_id $checkbox[$i];
            
    $sql "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
            
    $result mysql_query($sql) or die (mysql_error());
        }
        
        if(
    $result) {
            echo 
    'Rows deleted!
                <br />
            <a href="'
    .$_SERVER['REQUEST_URI'].'">Back</a>';
        }
    }

    mysql_close();
    ?>
    The problem I am having is that when I hit the "delete selected" button, nothing happens. Also, I would like to know what exactly this code does/means:
    PHP Code:
    for($i=0;$i<=$count;$i++) { 
    I have seen it lots of times, but can't quite figure out what $i is for. I appreciate any help

  2. #2
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Does anyone know what could be wrong, I find it hard to believe that no one has ever done something like this
    Thanks DD, you saved me countless times

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Assuming you have either register globals set to "On" or you assigned the variable earlier in the script, try changing the below:

    Code:
         for($i=0;$i<=$count;$i++) {
            $del_id = $checkbox[$i];
            $sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
            $result = mysql_query($sql) or die (mysql_error());
        }
    to this:

    Code:
         foreach ($checkbox as $del_id) {
            $sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
            $result = mysql_query($sql) or die (mysql_error());
        }
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Assuming you have either register globals set to "On" or you assigned the variable earlier in the script, try changing the below:

    Code:
         for($i=0;$i<=$count;$i++) {
            $del_id = $checkbox[$i];
            $sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
            $result = mysql_query($sql) or die (mysql_error());
        }
    to this:

    Code:
         foreach ($checkbox as $del_id) {
            $sql = "DELETE FROM `m_delete` WHERE `id` = '$del_id'";
            $result = mysql_query($sql) or die (mysql_error());
        }
    Hope this helps.
    I did that and when I hit delete, I get the error:
    Warning: Invalid argument supplied for foreach() in /home/bntqann/public_html/testing/login/admin/index.php on line 112. Line 112 is the foreach statement you told me to use. Any idea whats wrong?
    Thanks DD, you saved me countless times

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by thetestingsite
    Assuming you have either register globals set to "On" or you assigned the variable earlier in the script
    Did you check any of these things? If Register Globals is set to On, then the script should work. However; according to the error message you get, it seems that the variable $checkbox is not defined. So maybe even try changing (in the foreach loop) $checkbox to $_REQUEST['checkbox'] and see if that fixes it.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Something else I forgot to add is that you need to have a value for checkbox in your form. For example, where you have this:

    Code:
    <input type="checkbox" name="checkbox[]" />
    should be like this:

    Code:
    <input type="checkbox" name="checkbox[]" value="'.$id.'" />
    Sorry, didn't notice that before.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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