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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2009
    Location
    Indore (INDIA)
    Posts
    37
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Delete checked row from Database on Click of button

    Hello all,

    There is a 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>
    
    // 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 12:49 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Try something like
    PHP Code:
    if(isset($_POST['checkbox']) && count($_POST['checkbox'])>0){
    $list="("implode(","$_POST['checkbox']).")";

    $sql="delete from table_name where id in $list";

    PS: You need to validate your data to prevent sql injections.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    As a probably minor caution: Abdurooft's answer assumes that the id field in your table is a NUMERIC (probably INT) field. If you use text id's instead of just numbers, his code needs minor mods.

  • #4
    New Coder
    Join Date
    Mar 2009
    Location
    Michigan
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by abduraooft View Post
    PS: You need to validate your data to prevent sql injections.
    I'm not seeing how you can SQL inject through checkboxes, considering the only value obtained from their script is whether or not a checkbox is selected. However, I'm sure other parts of the page may have areas for text input, in which case this is solid advice.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by Shinykirby View Post
    I'm not seeing how you can SQL inject through checkboxes, considering the only value obtained from their script is whether or not a checkbox is selected. However, I'm sure other parts of the page may have areas for text input, in which case this is solid advice.
    Client side code can be easily edited by running some javascript code in the address bar. Aren't you familiar with tools like firebug?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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