How to delete a set of entries from the mysql table?

Discussion in 'PHP' started by Sangeetha.M, Nov 28, 2008.

  1. #1
    Hi everybody,

    Like in gmail, i would like to select some set of data's using checkbox and to delete it from the mysql table...i tried this using arrays but its not working...could anyone of you please help out in this...

    Thanks in advance
     
    Sangeetha.M, Nov 28, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    use as input an array for example

    <input type="checkbox" name="fileid[]" value="id1"> check box 1
    <input type="checkbox" name="fileid[]" value="id2"> check box 1
    <input type="checkbox" name="fileid[]" value="id3"> check box 1

    if you submit the form, loop fileid array example:

    
    if (isSet($_POST['fileid']) && is_array($_POST['fileid']))
    {
        foreach ($_POST['fileid'] AS $num => $value)
        {
        // $value for deletion
        }
    }
    
    Code (markup):
     
    EricBruggema, Nov 29, 2008 IP
  3. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Dear Sangeeta,
    Here is the error proof code

    // This is your php file that will u call when u hit submit button

    // file name is dodelete-php.php
    <?php
    require_once('connection.php');
    if (isset($_POST['del']) && is_array($_POST['del']))
    {
    foreach ($_POST['del'] as $num => $value)
    {
    $sql="delete from $tbl_name where id='$value'";
    $result=mysql_query($sql);
    }
    }
    header("location:redirect to ur file.php");
    ?>

    /* And here is for example your webpage that contains values with checkboxes that u many want to delete*/

    <html>
    <head>

    <title>Multiple Deletion Through PHP By Adeel Fakhar</title>
    <?php
    require_once('connection.php');
    $sql="select * from $tbl_name";
    $result=mysql_query($sql);
    ?>
    </head>

    <body>
    <table width="600" cellpadding="2" cellspacing="2">
    <tr>

    <form name="form1" method="post" action="dodelete-php.php">
    <tr bgcolor="#eaeaea">

    <th>
    id
    </th>
    <th>
    Name
    </th>
    <th>
    Father Name
    </th>
    <th>&nbsp;

    </th>
    <th>&nbsp;

    </th>
    </tr>
    <?php
    while($row=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td align="center">
    <?php
    echo $row[0];
    ?>
    </td>
    <td align="center">
    <?php
    echo $row[1];
    ?>
    </td>
    <td align="center">
    <?php
    echo $row[2];
    ?>
    </td>
    <td align="center">
    <input type="checkbox" name="del[]" value="<?php
    echo $row[0];
    ?>">
    </td>

    </tr>
    <?php
    }
    ?>

    <tr>

    <td colspan="3">
    <input type="submit" value="Delete">
    </td>
    </tr>
    </form>
    </table>

    </body>
    </html>
     
    mrmaf, Nov 29, 2008 IP