Deleting multiple Rows Using Checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arizal
    New Member
    • Feb 2007
    • 25

    #1

    Deleting multiple Rows Using Checkbox

    Hi ,
    I am trying to figure out why my PHP code to delete multiple value by selecting the checkbox is not working. My php code is listed as below

    [PHP]
    <?php
    $sql = "Select * from list;";
    $result=mysql_q uery($sql) or die ("<br> Could not Retrieve Data <br>");
    $num_rows=mysql _num_rows($resu lt);

    While ($row =mysql_fetch_ar ray($rs))
    {
    $currentCom_id = $row["com_id"];
    ?>

    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['com_id']; ?>">
    .....
    .....
    <?php

    }
    ?>

    <input name="delete" type="submit" id="delete" value="Delete">
    [/PHP]

    Now for deleting the selected checkbox i have used following SQL queries.

    [PHP]

    if($_POST['delete'] == "Delete"){
    for($j=0;$j<$nu m_rows;$j++){
    $del_id = $checkbox[$j];
    $sql = "DELETE FROM committee WHERE com_id='$del_id '";
    $result = mysql_query($sq l);
    echo $result;
    }
    }
    mysql_close();
    ?>

    [/PHP]

    The problem is when i press delete it does not deletes the rows selected, else it just reloads the page. I dont know where i am wrong.

    can u guyz please help me on this ...
    THanks
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Looks like you are expecting the GLOBAL post vars to be switched on. It is switched off by default in later versions of php. You should not use this anyway. You need to read the checkbox values through the $_POST array

    Comment

    • forjaes
      New Member
      • Jun 2007
      • 1

      #3
      Hi,

      i'm gaing the same problem... I change my code to that:

      <?
      // Check if delete button active, start this
      if($_POST['delete'] == 'Delete'){
      for($i=0;$i<$co unt;$i++){
      $del_id = $_POST['checkbox'][$i];
      $sql = "DELETE FROM $tbl_name WHERE userid='$del_id '";
      $result = mysql_query($sq l);
      }


      // if successful redirect to delete_multiple .php
      if($result){
      echo "<meta http-equiv=\"refresh \" content=\"0;URL =delete_multipl e.php\">";
      }
      }
      mysql_close();
      ?>


      But it still doesn't work...

      Please help.
      thx.

      Comment

      • epots9
        Recognized Expert Top Contributor
        • May 2007
        • 1352

        #4
        what errors are u getting?

        Comment

        • rkarim1981
          New Member
          • Feb 2008
          • 1

          #5
          Hi, If any one didn't got the solution yet of this problem and still seeking! Please mail me on DELETED
          Last edited by RedSon; Feb 13 '08, 02:50 PM. Reason: No Email Allowed!

          Comment

          Working...