删除复选框值,但是当我单击按钮页面刷新时

时间:2014-12-12 08:58:41

标签: php database checkbox mysqli

嘿,我写这段代码是为了从数据库中删除选中的复选框值。是的我知道这里有一些例子。但是,当我看到他们并试图按照他们在他们的问题中所做的那样做。仍然没有取得成功。你能帮我解决这个问题。当我点击删除按钮时页面刷新并显示消息成功删除记录但之后没有看到任何记录从数据库中删除

<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");  // Connect to database

                        //$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   } ?>
                    </table>
                        <input type='submit' name='delete' id='delete' value='Delete'>
                        <?php
                              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>

1 个答案:

答案 0 :(得分:0)

在连接到数据库行后立即移动此块: