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 2 of 2
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    texas
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Deleting items that are checked

    I have looked for this and tried to find examples of what to do and I have not found anything really. I am trying to delete all of the items that are checked with a checkbox. I found a site: http://www.phpeasystep.com/mysqlview.php?id=8

    Sorry if I was not supposed to post a link, I figured I would give credit to this person...

    I did exactly what they said and it still does not work. Can you look at what I did and tell me what I did?

    My code is posted:
    Code:
    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="password"; // Mysql password
    $db_name="information"; // Database name
    $tbl_name="test_mysql"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    
    $count=mysql_num_rows($result);
    
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
    </tr>
    <tr>
    <td align="center" bgcolor="#FFFFFF">#</td>
    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?PHP echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFFF"><?PHP echo $rows['id']; ?></td>
    <td bgcolor="#FFFFFF"><?PHP echo $rows['name']; ?></td>
    <td bgcolor="#FFFFFF"><?PHP echo $rows['lastname']; ?></td>
    <td bgcolor="#FFFFFF"><?PHP echo $rows['email']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?PHP 
    // Check if delete button active, start this
    if($delete){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $result = mysql_query($sql);
    }
    
    // if successful redirect to delete_multiple.php
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
    }
    }
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
    thanks
    bri

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    That is a fairly crappy tutorial that relies on register_globals being enabled. It also sends a separate query for every item checked, when it could easily be handled with a single query.

    Replace the entire if ($delete) {} block with this:
    PHP Code:
    if ( isset( $_POST['checkbox'] ) && is_array$_POST['checkbox'] ) )
    {
        
    $ids    array_map'intval'$_POST['checkbox'] );
        
    $ids    implode','$ids );
        
    $sql    'DELETE FROM `table_name` WHERE `id` IN ( ' $ids ' )';
        
    $result mysql_query$sql );
    }
    else
    {
        
    $result FALSE;

    ...or:
    PHP Code:
    $result = ( isset( $_POST['checkbox'] ) && is_array$_POST['checkbox'] ) ) ? mysql_query'DELETE FROM `table_name` WHERE `id` IN ( ' implode','array_map'intval'$_POST['checkbox'] ) ) . ' )' ) : FALSE
    Same thing, just streamlined.
    Last edited by kbluhm; 05-21-2008 at 04:47 PM.


  •  

    Posting Permissions

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