Delete Entry from database on clicking delete button
<p>I have added a delete button on my point of sale module.In purchase tab when i click delete it doesn't delete my entry from database.I need help for deleting purchase entry from database whenever i click on delete button.</p>

<pre><code>&lt;?php
include_once("init.php");

?&gt;
&lt;!DOCTYPE html&gt;

&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;POSNIC - Stock&lt;/title&gt;

    &lt;!-- Stylesheets --&gt;
    &lt;!----&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;

    &lt;!-- Optimize for mobile devices --&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"/&gt;

    &lt;!-- jQuery &amp; JS files --&gt;
    &lt;?php include_once("tpl/common_js.php"); ?&gt;
    &lt;script src="js/script.js"&gt;&lt;/script&gt;


    &lt;script LANGUAGE="JavaScript"&gt;
        &lt;!--
        // Nannette Thacker http://www.shiningstar.net
        function confirmSubmit() {
            var agree = confirm("Are you sure you wish to Delete this Entry?");
            if (agree)
                return true;
            else
                return false;
        }

        function confirmDeleteSubmit() {
            var flag = 0;
            var field = document.forms.deletefiles;
            for (i = 0; i &lt; field.length; i++) {
                if (field[i].checked == true) {
                    flag = flag + 1;

                }

            }
            if (flag &lt; 1) {
                alert("You must check one and only one checkbox!");
                return false;
            } else {
                var agree = confirm("Are you sure you wish to Delete Selected Record?");
                if (agree)

                    document.deletefiles.submit();
                else
                    return false;

            }
        }
        function confirmLimitSubmit() {
            if (document.getElementById('search_limit').value != "") {

                document.limit_go.submit();

            } else {
                return false;
            }
        }


        function checkAll() {

            var field = document.forms.deletefiles;
            for (i = 0; i &lt; field.length; i++)
                field[i].checked = true;
        }

        function uncheckAll() {
            var field = document.forms.deletefiles;
            for (i = 0; i &lt; field.length; i++)
                field[i].checked = false;
        }
        // --&gt;
    &lt;/script&gt;
    &lt;script&gt;


        /*$.validator.setDefaults({
         submitHandler: function() { alert("submitted!"); }
         });*/
        $(document).ready(function () {

            // validate signup form on keyup and submit
            $("#form1").validate({
                rules: {
                    name: {
                        required: true,
                        minlength: 3,
                        maxlength: 200
                    },
                    address: {
                        minlength: 3,
                        maxlength: 500
                    },
                    contact1: {
                        minlength: 3,
                        maxlength: 20
                    },
                    contact2: {
                        minlength: 3,
                        maxlength: 20
                    }
                },
                messages: {
                    name: {
                        required: "Please enter a supplier Name",
                        minlength: "supplier must consist of at least 3 characters"
                    },
                    address: {
                        minlength: "supplier Address must be at least 3 characters long",
                        maxlength: "supplier Address must be at least 3 characters long"
                    }
                }
            });

        });

    &lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;!-- TOP BAR --&gt;
&lt;?php include_once("tpl/top_bar.php"); ?&gt;
&lt;!-- end top-bar --&gt;


&lt;!-- HEADER --&gt;
&lt;div id="header-with-tabs"&gt;

    &lt;div class="page-full-width cf"&gt;

        &lt;ul id="tabs" class="fl"&gt;
            &lt;li&gt;&lt;a href="dashboard.php" class="dashboard-tab"&gt;Dashboard&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_sales.php" class="sales-tab"&gt;Sales&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_customers.php" class=" customers-tab"&gt;Customers&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_purchase.php" class="active-tab purchase-tab"&gt;Purchase&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_supplier.php" class=" supplier-tab"&gt;Supplier&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_product.php" class=" stock-tab"&gt;Stocks / Products&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_payments.php" class="payment-tab"&gt;Payments / Outstandings&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;a href="view_report.php" class="report-tab"&gt;Reports&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
        &lt;!-- end tabs --&gt;

        &lt;!-- Change this image to your own company's logo --&gt;
        &lt;!-- The logo will automatically be resized to 30px height. --&gt;
        &lt;a href="#" id="company-branding-small" class="fr"&gt;&lt;img src="&lt;?php if (isset($_SESSION['logo'])) {
                echo "upload/" . $_SESSION['logo'];
            } else {
                echo "upload/posnic.png";
            } ?&gt;" alt="Point of Sale"/&gt;&lt;/a&gt;

    &lt;/div&gt;
    &lt;!-- end full-width --&gt;

&lt;/div&gt;
&lt;!-- end header --&gt;


&lt;!-- MAIN CONTENT --&gt;
&lt;div id="content"&gt;

    &lt;div class="page-full-width cf"&gt;

        &lt;div class="side-menu fl"&gt;

            &lt;h3&gt;Stock Purchase&lt;/h3&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href="add_purchase.php"&gt;Add Purchase&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href="view_purchase.php"&gt;View Purchase &lt;/a&gt;&lt;/li&gt;

            &lt;/ul&gt;
        &lt;/div&gt;
        &lt;!-- end side-menu --&gt;

        &lt;div class="side-content fr"&gt;

            &lt;div class="content-module"&gt;

                &lt;div class="content-module-heading cf"&gt;

                    &lt;h3 class="fl"&gt;Purchase&lt;/h3&gt;
                    &lt;span class="fr expand-collapse-text"&gt;Click to collapse&lt;/span&gt;
                    &lt;span class="fr expand-collapse-text initial-expand"&gt;Click to expand&lt;/span&gt;

                &lt;/div&gt;
                &lt;!-- end content-module-heading --&gt;

                &lt;div class="content-module-main cf"&gt;


                    &lt;table&gt;
                        &lt;form action="" method="post" name="search"&gt;
                            &lt;input name="searchtxt" type="text" class="round my_text_box" placeholder="Search"&gt;
                            &amp;nbsp;&amp;nbsp;&lt;input name="Search" type="submit" class="my_button round blue   text-upper"
                                               value="Search"&gt;
                        &lt;/form&gt;
                        &lt;form action="" method="get" name="limit_go"&gt;
                            Page per Record&lt;input name="limit" type="text" class="round my_text_box" id="search_limit"
                                                  style="margin-left:5px;"
                                                  value="&lt;?php if (isset($_GET['limit'])) echo $_GET['limit']; else echo "10"; ?&gt;"
                                                  size="3" maxlength="3"&gt;
                            &lt;input name="go" type="button" value="Go" class=" round blue my_button  text-upper"
                                   onclick="return confirmLimitSubmit()"&gt;
                        &lt;/form&gt;

                        &lt;form name="deletefiles" action="delete.php" method="post"&gt;

                            &lt;input type="hidden" name="table" value="stock_entries"&gt;
                            &lt;input type="hidden" name="return" value="view_purchase.php"&gt;
                            &lt;input type="button" name="selectall" value="SelectAll"
                                   class="my_button round blue   text-upper" onClick="checkAll()"
                                   style="margin-left:5px;"/&gt;
                            &lt;input type="button" name="unselectall" value="DeSelectAll"
                                   class="my_button round blue   text-upper" onClick="uncheckAll()"
                                   style="margin-left:5px;"/&gt;
                            &lt;input name="dsubmit" type="button" value="Delete Selected"
                                   class="my_button round blue   text-upper" style="margin-left:5px;"
                                   onclick="return confirmDeleteSubmit()"/&gt;


                            &lt;table&gt;
                                &lt;?php


                                $SQL = "SELECT DISTINCT(stock_id) FROM  stock_entries where type='entry'";

                                if (isset($_POST['Search']) AND trim($_POST['searchtxt']) != "") {

                                    $SQL = "SELECT DISTINCT(stock_id) FROM  stock_entries WHERE stock_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_supplier_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_id LIKE '%" . $_POST['searchtxt'] . "%' OR date LIKE '%" . $_POST['searchtxt'] . "%' OR type LIKE '%" . $_POST['searchtxt'] . "%' AND type='entry'";


                                }

                                $tbl_name = "stock_entries";        //your table name

                                // How many adjacent pages should be shown on each side?

                                $adjacents = 3;


                                /*

                                   First get total number of rows in data table.

                                   If you have a WHERE clause in your query, make sure you mirror it here.

                                */

                                $query = "SELECT COUNT(DISTINCT stock_id) as num FROM $tbl_name where type='entry'";
                                if (isset($_POST['Search']) AND trim($_POST['searchtxt']) != "") {

                                    $query = "SELECT COUNT(DISTINCT stock_id) as num FROM stock_entries WHERE stock_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_supplier_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_id LIKE '%" . $_POST['searchtxt'] . "%' OR date LIKE '%" . $_POST['searchtxt'] . "%' OR type LIKE '%" . $_POST['searchtxt'] . "%' and type='entry'";


                                }
                                $total_pages = mysqli_fetch_array(mysqli_query($db-&gt;connection, $query));

                                $total_pages = $total_pages['num'];


                                /* Setup vars for query. */

                                $targetpage = "view_stock_entries.php";    //your file name  (the name of this file)

                                $limit = 10;                                //how many items to show per page
                                if (isset($_GET['limit']))
                                    $limit = $_GET['limit'];

                                $page = isset($_GET['page']) ? $_GET['page'] : 0;

                                if ($page)

                                    $start = ($page - 1) * $limit;            //first item to display on this page

                                else

                                    $start = 0;                                //if no page var is given, set start to 0


                                /* Get data. */

                                $sql = "SELECT DISTINCT(stock_id) FROM stock_entries where type='entry' ORDER BY date desc LIMIT $start, $limit  ";

                                if (isset($_POST['Search']) AND trim($_POST['searchtxt']) != "") {

                                    $sql = "SELECT DISTINCT(stock_id) FROM stock_entries WHERE stock_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_supplier_name LIKE '%" . $_POST['searchtxt'] . "%' OR stock_id LIKE '%" . $_POST['searchtxt'] . "%' OR date LIKE '%" . $_POST['searchtxt'] . "%' OR type LIKE '%" . $_POST['searchtxt'] . "%' and type='entry' ORDER BY date desc LIMIT $start, $limit";


                                }

                                $result = mysqli_query($db-&gt;connection, $sql);


                                /* Setup page vars for display. */

                                if ($page == 0) $page = 1;                    //if no page var is given, default to 1.

                                $prev = $page - 1;                            //previous page is page - 1

                                $next = $page + 1;                            //next page is page + 1

                                $lastpage = ceil($total_pages / $limit);        //lastpage is = total pages / items per page, rounded up.

                                $lpm1 = $lastpage - 1;                        //last page minus 1


                                /*

                                    Now we apply our rules and draw the pagination object.

                                    We're actually saving the code to a variable in case we want to draw it more than once.

                                */

                                $pagination = "";

                                if ($lastpage &gt; 1) {

                                    $pagination .= "&lt;div &gt;";

                                    //previous button

                                    if ($page &gt; 1)

                                        $pagination .= "&lt;a href=\"view_purchase.php?page=$prev&amp;limit=$limit\" class=my_pagination &gt;Previous&lt;/a&gt;";

                                    else

                                        $pagination .= "&lt;span class=my_pagination&gt;Previous&lt;/span&gt;";


                                    //pages

                                    if ($lastpage &lt; 7 + ($adjacents * 2))    //not enough pages to bother breaking it up

                                    {

                                        for ($counter = 1; $counter &lt;= $lastpage; $counter++) {

                                            if ($counter == $page)

                                                $pagination .= "&lt;span class=my_pagination&gt;$counter&lt;/span&gt;";

                                            else

                                                $pagination .= "&lt;a href=\"view_purchase.php?page=$counter&amp;limit=$limit\" class=my_pagination&gt;$counter&lt;/a&gt;";

                                        }

                                    } elseif ($lastpage &gt; 5 + ($adjacents * 2))    //enough pages to hide some

                                    {

                                        //close to beginning; only hide later pages

                                        if ($page &lt; 1 + ($adjacents * 2)) {

                                            for ($counter = 1; $counter &lt; 4 + ($adjacents * 2); $counter++) {

                                                if ($counter == $page)

                                                    $pagination .= "&lt;span class=my_pagination&gt;$counter&lt;/span&gt;";

                                                else

                                                    $pagination .= "&lt;a href=\"view_purchase.php?page=$counter&amp;limit=$limit\" class=my_pagination&gt;$counter&lt;/a&gt;";

                                            }

                                            $pagination .= "...";

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=$lpm1&amp;limit=$limit\" class=my_pagination&gt;$lpm1&lt;/a&gt;";

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=$lastpage&amp;limit=$limit\" class=my_pagination&gt;$lastpage&lt;/a&gt;";

                                        } //in middle; hide some front and some back

                                        elseif ($lastpage - ($adjacents * 2) &gt; $page &amp;&amp; $page &gt; ($adjacents * 2)) {

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=1&amp;limit=$limit\" class=my_pagination&gt;1&lt;/a&gt;";

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=2&amp;limit=$limit\" class=my_pagination&gt;2&lt;/a&gt;";

                                            $pagination .= "...";

                                            for ($counter = $page - $adjacents; $counter &lt;= $page + $adjacents; $counter++) {

                                                if ($counter == $page)

                                                    $pagination .= "&lt;span  class=my_pagination&gt;$counter&lt;/span&gt;";

                                                else

                                                    $pagination .= "&lt;a href=\"view_purchase.php?page=$counter&amp;limit=$limit\" class=my_pagination&gt;$counter&lt;/a&gt;";

                                            }

                                            $pagination .= "...";

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=$lpm1&amp;limit=$limit\" class=my_pagination&gt;$lpm1&lt;/a&gt;";

                                            $pagination .= "&lt;a href=\"view_purchase.php?page=$lastpage&amp;limit=$limit\" class=my_pagination&gt;$lastpage&lt;/a&gt;";

                                        } //close to end; only hide early pages

                                        else {

                                            $pagination .= "&lt;a href=\"$view_purchase.php?page=1&amp;limit=$limit\" class=my_pagination&gt;1&lt;/a&gt;";

                                            $pagination .= "&lt;a href=\"$view_purchase.php?page=2&amp;limit=$limit\" class=my_pagination&gt;2&lt;/a&gt;";

                                            $pagination .= "...";

                                            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter &lt;= $lastpage; $counter++) {

                                                if ($counter == $page)

                                                    $pagination .= "&lt;span class=my_pagination &gt;$counter&lt;/span&gt;";

                                                else

                                                    $pagination .= "&lt;a href=\"$targetpage?page=$counter&amp;limit=$limit\" class=my_pagination&gt;$counter&lt;/a&gt;";

                                            }

                                        }

                                    }


                                    //next button

                                    if ($page &lt; $counter - 1)

                                        $pagination .= "&lt;a href=\"view_purchase.php?page=$next&amp;limit=$limit\" class=my_pagination&gt;Next&lt;/a&gt;";

                                    else

                                        $pagination .= "&lt;span class= my_pagination &gt;Next&lt;/span&gt;";

                                    $pagination .= "&lt;/div&gt;\n";

                                }

                                ?&gt;
                                &lt;tr&gt;
                                    &lt;th&gt;No&lt;/th&gt;
                                    &lt;th&gt;Stock Id&lt;/th&gt;
                                    &lt;th&gt;Stock Name&lt;/th&gt;
                                    &lt;th&gt;Date&lt;/th&gt;
                                    &lt;th&gt;Supplier&lt;/th&gt;
                                    &lt;th&gt;Amount&lt;/th&gt;
                                    &lt;th&gt;Edit /Delete&lt;/th&gt;
                                    &lt;th&gt;Select&lt;/th&gt;
                                    &lt;td&gt;&lt;/td&gt;
                                &lt;/tr&gt;

                                &lt;?php $i = 1;
                                $no = $page - 1;
                                $no = $no * $limit;

                                while ($row = mysqli_fetch_array($result)) {

                                    $entryid = $row['stock_id'];
                                    $line = $db-&gt;queryUniqueObject("SELECT * FROM stock_entries WHERE stock_id='$entryid' ");

                                    $mysqldate = $line-&gt;date;

                                    $phpdate = strtotime($mysqldate);

                                    $phpdate = date("d/m/Y", $phpdate);

                                    ?&gt;

                                    &lt;tr&gt;

                                        &lt;td&gt;&lt;/td&gt;

                                        &lt;td width="100"&gt;&lt;?php echo $line-&gt;stock_id; ?&gt;&lt;/td&gt;
                                        &lt;td width="100"&gt;&lt;?php echo $line-&gt;stock_name; ?&gt;&lt;/td&gt;


                                        &lt;td width="100"&gt;&lt;?php echo $phpdate; ?&gt;&lt;/td&gt;

                                        &lt;td width="100"&gt;&lt;?php echo $line-&gt;stock_supplier_name; ?&gt;&lt;/td&gt;
                                        &lt;td width="100"&gt;&lt;?php echo $line-&gt;subtotal; ?&gt;&lt;/td&gt;


                                        &lt;td&gt;
                                            &lt;a href="update_purchase.php?sid=&lt;?php echo $entryid; ?&gt;&amp;table=stock_entries&amp;return=view_purchase.php"
                                               class="table-actions-button ic-table-edit"&gt;
                                            &lt;/a&gt;
                                            &lt;a onclick="return confirmSubmit()"
                                               href="delete.php?id=&lt;?php echo isset($row['id']) ? $row['id'] : 0; ?&gt;&amp;table=stock_entries&amp;return=view_purchase.php"
                                               class="table-actions-button ic-table-delete"&gt;&lt;/a&gt;
                                        &lt;/td&gt;
                                        &lt;td&gt;&lt;input type="checkbox"
                                                   value="&lt;?php echo isset($row['id']) ? $row['id'] : 0; ?&gt;"
                                                   name="checklist[]" id="check_box"/&gt;&lt;/td&gt;
                                        &lt;td&gt;&lt;/td&gt;
                                    &lt;/tr&gt;
                                    &lt;?php $i++;
                                } ?&gt;
                                &lt;tr&gt;

                                    &lt;td align="center"&gt;
                                        &lt;div style="margin-left:20px;"&gt;&lt;?php echo $pagination; ?&gt;&lt;/div&gt;
                                    &lt;/td&gt;

                                &lt;/tr&gt;
                            &lt;/table&gt;
                        &lt;/form&gt;


                &lt;/div&gt;
            &lt;/div&gt;



&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>delete.php</p>

<pre><code>&lt;?php
include_once "init.php";
// Use session variable on this page. This function must put on the top of page.
if (!isset($_SESSION['username']) || $_SESSION['usertype'] != 'admin') { // if session variable "username" does not exist.
    header("location: index.php?msg=Please%20login%20to%20access%20admin%20area%20!"); // Re-direct to index.php
} else {

    error_reporting(0);
    if (isset($_GET['id']) &amp;&amp; isset($_GET['table'])) {
        $id = $_GET['id'];
        $tablename = $_GET['table'];
        $return = $_GET['return'];

        if ($tablename == "stock_entries") {
            $difference = $db-&gt;queryUniqueValue("SELECT quantity FROM stock_entries WHERE id=$id");

            $name = $db-&gt;queryUniqueValue("SELECT stock_name FROM stock_entries WHERE id=$id");
            $result = $db-&gt;query("SELECT * FROM stock_entries where id &gt; $id");
            while ($line2 = $db-&gt;fetchNextObject($result)) {
                $osd = $line2-&gt;opening_stock - $difference;
                $csd = $line2-&gt;closing_stock - $difference;
                $cid = $line2-&gt;id;
                $db-&gt;execute("UPDATE stock_entries SET opening_stock=" . $osd . ",closing_stock=" . $csd . " WHERE id=$cid");

            }
            $total = $db-&gt;queryUniqueValue("SELECT quantity FROM stock_avail WHERE name='$name'");
            $total = $total - $difference;
            $db-&gt;execute("UPDATE stock_avail SET quantity=$total WHERE name='$name'");
        }

        $id = $_GET['id'];

        $db-&gt;execute("DELETE FROM $tablename WHERE id=$id");

        header("location: $return?msg=Record Deleted Successfully!&amp;id=$id");
    }
    if (isset($_POST['table']) &amp;&amp; isset($_POST['checklist'])) {
        $data = $_REQUEST['checklist'];
        $tablename = $_POST['table'];
        $return = $_REQUEST['return'];
        for ($i = 0; $i &lt; count($data); $i++) {
            $db-&gt;execute("DELETE FROM $tablename WHERE id=$data[$i]");
        }
        header("location: $return?msg=Record Deleted Successfully!");
    }
    if (isset($_POST['return'])) {
        header("location: $return");
    }
}
?&gt;
</code></pre>