Multiple Delete (PHP)

Now I will try to share a little about how to create a facility to delete a record on the website (php) similar to delete messages in GMail.

  1. First, create a form and then show a list of records retrieved from the database. View in a table where each record by 1 checkbox. 
    <?
    
           $rs=mysql_query("select * from mhs");//ambil record dari tabel mhs
    
           while($data=mysql_fetch_array($rs)){?>
    
              <tr>
    
                 <td>
    
                  <input type="checkbox" name="chk[]" value="<?=$data[id]?>">
    
                 </td>
    
                  <td><?=$data[id]?></td>
    
                  <td><?=$data[nim]?></td>
    
                  <td><?=$data[nama]?></td>
    
               </tr><?
    
           }
    
    ?>

    Checkbox on top of each filled with the id of each record and the name of the checkbox by the same name and added symbol behind the name of “[]”. This is so that when read by PHP, Checkbox considered as an array.

  2. <?
    
              $chk=$_POST[chk];
    
              / * -------------- Change corresponding computer settings masing2 ---- * /
    
              $host="localhost";
    
              $user="root";
    
              $pass="";
    
             /*--------------------------------------------------------*/
    
              $ Con = mysql_connect ($ host, $ user, $ pass); // open connection to the database
    
              mysql_select_db("tes");//pilih database
    
     
              foreach($chk as $id){
    
                  if(mysql_query("delete from mhs where id=".$id)){
    
                      echo "<div align=center>
    
                              Remove record with id = ". $ Id." - Success
    
                            </div><br>";
    
                  }else{
    
                      echo "<div align=center>
    
                              Delete record with id=".$id."-- Failed
    
                            </div><br>";
    
                  }     
    
              }
    
        mysql_close($con);
    
    ?>

     After that create a new php file to delete process.

    First, do any data retrieval checkbox marked. As already known, to take the value sent from a form can use the $ _POST , $ _GET , or $ _REQUEST . Well, to take a checkbox value is the same way, here I am using the POST method.

           $chk=$_POST[chk];

     Because chk on html form written like this ” chk [] ” then the current value of the $ _POST pemgambilan then php will directly menggangap chk it as an array. Then the variable $ chk now be an array that contains the value of checkbox- checkbox is selected / checked.

    When he could take valuenya, then stay manipulate value-value is taken as necessary (in this example we will use valuenya as key to delete the record from the database. It can be seen from the above source code, I use foreach to take one-on-one contents array (array content is id which records obtained from the checkbox is checked only). Later in the recurrence did query delete :

    foreach($chk as $id){
    
        if(mysql_query("delete from mhs where id=".$id)){
    
            echo "<div align=center>
    
                     Remove record with id = ". $ Id." - Success
    
                 </div><br>";
    
        }else{
    
            echo "<div align=center>
    
                     Delete record with id=".$id."-- Failed
    
                  </div><br>";
    
        }     
    
    }

     

Leave a Reply

Your email address will not be published. Required fields are marked *