Hey I write this code for delete the selected check box values from the database. Yeah I know there are some example already here. But when I take look on them and tried to do as per they done in their question. still doesn't get the success. Can you guys help me with this query. When I click on the on the delete button the page is refresh and message display that records are delete successfully but after that not see any records delete from database

<html>
    <head>
        <title>
        </title>
        <meta name="" content="">
    </head>
    <body bgcolor="#05fa8c">
        <form action="a.php" method="POST">
            <table style="margin-top: 252px; font-size: larger; font-style: oblique;" align="center" bgcolor="#05fa8c">
                <?php
                    include("config.php");

                        //$id = $_POST['id'];

                        $sbook = "SELECT * FROM `book`";
                        $rnq   = mysqli_query($con,$sbook);
                        $count = mysqli_num_rows($rnq);

                        if(!$rnq)
                        {
                            print ("Error: %s\n ". mysqli_error($con));
                        }

                        echo "<table border=2 table style=margin-top: 252px; font-size: larger; font-style: oblique; bgcolor=#05fa8c>
                        <tr>
                        <td width=10% height= 10%><b>Book Name:</b></td>
                        <td width=10% height= 10%><b>Author Name:</b></td>
                        <td width=10% height= 10%><b>Publisher Name:</b></td>
                        <td width=10% height= 10%><b>Description</b></td>
                        <td width=3% height= 3% align='center'><b>#</b></td>
                        </tr>";

                        while($row = mysqli_fetch_array($rnq))
                        {
                            $rid   = $row['id'];
                            $bname = $row['bname'];
                            $aname = $row['aname'];
                            $pname = $row['pname'];
                            $desc  = $row['description'];

                            echo "<tr>
                            <td width=10% height= 10%><b>$bname</b></td>
                            <td width=10% height= 10%><b>$aname</b></td>
                            <td width=10% height= 10%><b>$pname</b></td>
                            <td width=10% height= 10%><b>$desc</b></td>";?>
                            <td align='center'><input type="checkbox" name="checkbox[<? echo $row['id']; ?>]" id="checkbox[<? echo $row['id']; ?>]" value="<? echo $row['id']; ?>"></td>
                    <?php    } 
                    echo "</table>";
                    echo "<input type='submit' name='delete' id='delete' value='Delete' >";

                    if(isset($_POST['delete'])){
                        foreach($_POST['checkbox'] as $del_id){
                            $del_id = (int)$del_id;
                            $sql = 'DELETE FROM `book` WHERE `id` = '.$del_id;
                            mysqli_query($con, $sql);
                            echo "Records delete successfully";
                        }
                    }   
            ?>
        </form>
    </body>
</html>

Recommended Answers

All 2 Replies

I have tested your script and it works OK. This is the data I tested on:

INSERT INTO book (id, bname, aname, pname, description) VALUES 
(1, 'Book 1', 'Author 1', 'Publisher 1', 'Description 1'),
(2, 'Book 2', 'Author 2', 'Publisher 2', 'Description 2'),
(3, 'Book 3', 'Author 3', 'Publisher 3', 'Description 3'),
(4, 'Book 4', 'Author 4', 'Publisher 4', 'Description 4')

To make sure form submits to the same page I changed line 8 to:

<form action="#" method="POST">

What you can do is you can put an echo line just below line 54 and check what queries get generated:

echo "$sql<br>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.