Delete Multiple Data with PHP

Case Study : Create function to delete multiple data on the database with checkbox component
Requirements : Webserver Packages, already installed.

Multi-delete Checkbox like in Gmail, we can choose to delete a message in our inbox smply by clicking the checkbox on the data that we want to remove. This tutorial will create a sample application to remove the data – specific data which is marked by checkbox (like in Gmail).

Ok, let’s get into the tutorial.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    [sourcecode language=”php”]
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    or die ("Connect Failed !! : ".mysql_error());
  2. Save with the name connect.php

Step 4 : Script to Show the Data

  1. Type the following script,
    [sourcecode language=”php”]
    <script type="text/javascript" src="pilihan.js"></script>
    <script type="text/javascript" src="warning.js"></script>
    <h2> The Data <h2>
    include ‘connect.php’;
    $query = "SELECT * FROM tb_book"; //the query for get all data in tb_student
    $result = mysql_query($query);

    <form name="myform" method="post" action="prosesdeletemulti.php">

    echo "<table border=’0′ cellpadding=’2′ cellspacing=’2′>";
    echo "<tr bgcolor=’orange’>
    <td> <b> Title </b> </td>
    <td> <b> Author </b> </td>


    $i = 0;
    while ($data = mysql_fetch_array($result)) { //mysql_fetch_array = get the query data into array
    echo "<tr>
    <td>".$data[‘author’]."</td> "; ?>
    <td><input type="checkbox" name="id[<?php echo $i;?>]" value="<?php echo $data[‘id’];?>" /></td>
    echo "</table>";
    <td> <input type="submit" value="Delete" onClick="return warning();" name="submit">
    <input type="reset" value="Cancel" name="reset"> </td>

    </html> [/sourcecode]

  2. save with the name student_data1.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    [sourcecode language=”javascript”]
    function warning() {
    return confirm(‘Are You Sure to Delete This Data?’);
  2. save with the name warning.js

Step 6 : Javascript to get the checkbox ed component

  1. Type the following script,
    [sourcecode language=”javascript”]
    function pilihan()
    // read the component from ‘myform’
    var jumKomponen = document.myform.length;
    // no check all
    if (document.myform[0].checked == false)
    for (i=1; i<=jumKomponen; i++)
    if (document.myform[i].type == "checkbox") document.myform[i].checked = false;
  2. save with the name pilihan.js

Step 7 : Script to process delete multiple data

  1. Type the following script,
    [sourcecode language=”php”]
    include ‘connect.php’;
    $data = $_POST[‘id’]; //get all the id book that will be deleted

    foreach($data as $data1) { //looping according to the total data that checked
    //echo $data1;
    $query = "DELETE FROM tb_book where id = $data1"; //the query to delete data according to id
    $result = mysql_query($query);
    if ($result) {
    include "student_data1.php";
    echo "Delete sucess";

  2. save with the name prosesdeletemulti.php

Step 8 : Testing Code

  1. Go to http://localhost/tutorphp/student_data1.php. Check the checkbox on the data where you will delete the data.
  2. Warning appear. Choose Ok.
  3. Success confirmation appear, and the data really has been deleted. Check your database also 😀

For the script explanation.. see script comment. and feel free to comment 😀

OK. Haaaaaaaapppy Coding! 😀

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

2 replies on “Delete Multiple Data with PHP”

If you won the business enterprise purely on price you’ll have to be competitive.
If you have plans of engaging into a true estate transaction, it’s important that you discover how to differentiate the
two of them. Income Protection insurance cairns (Dalene) is available in two main varieties
of policies.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.