Results 1 to 2 of 2

Thread: Delete checked rows from Database on Click of button

  1. #1
    Join Date
    Feb 2009
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Delete checked rows from Database on Click of button

    Hello all,

    There is my table in database which contains persons info.

    A tabular view ("form1") is a Preview table Similar to original Database table, it retrieves the data from database & show the data recently inserted on Submit.

    here is some code

    Code:
    <?php
    
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    
    
    
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("mydbase")or die("cannot select DB");
    
    
    $sql="SELECT * FROM  mytbl";
    $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="600" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFFF"><strong>My Total Submitted Data</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>Age</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>City</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Country</strong></td>
    
    </tr>
    <?
    while($rows=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFFF"><? echo $rows['Id']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['Name']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['Age']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['City']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['Country']; ?></td>
    
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    
    </table>
    </form>
    </td>
    </tr>
    </table>
    
    // Check if delete button active, start this
    
    if(isset($_POST['delete']))
    {
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM mytbl WHERE id='$del_id' ";
    $result = mysql_query($sql) or die(mysql_error()); 
    if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
    }
    }
    
    mysql_close();
    ?>
    what i want is when any one select the check box in front of each data rows of "form1" table

    & click on "Delete" button displayed below on "form1" ,

    that particular selected rows to be deleted from the database &

    only remaining data rows in database to be shown in "form1" table.

    I have tried some sql queries of 'DELETE from table' but it doesn't work,

    So please suggest me code/sql that can be used ahead to delete the selected rows from Database .

    Thanks & Regards.
    Last edited by MSK7; 04-13-2009 at 11:47 AM.

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

    Default

    Change this:

    Code:
    if(isset($_POST['delete']))
    {
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM mytbl WHERE id='$del_id' ";
    $result = mysql_query($sql) or die(mysql_error()); 
    if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
    }
    }
    to:

    Code:
    if(isset($_POST['delete'])) {
     if (count($_POST['checkbox']) > 0) {
       foreach ($_POST['checkbox'] as $del_id) {
        $sql = "DELETE FROM mytbl WHERE id=' " . $del_id . " ' ";
        $result = mysql_query($sql) or die(mysql_error()); 
        if(mysql_affected_rows($result) > 0) echo 'Selected data rows Deleted';
      }
     }
    }
    Not tested, but should work. 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

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
  •