PHP复选框多次删除

时间:2011-12-15 15:01:00

标签: php post checkbox

我的实施似乎不起作用。你能指出可能是什么问题或者指出一个更好的解决方案吗?当我选中复选框并单击删除按钮时,它似乎没有做任何事情......请帮助我。

<div id="container" class="page">
    <img id="disclaimer" class="page" src="images/DISCLAIMER.png" alt="" />
    <img id="logo" class="page" src="images/MI-LOGO.png" alt="" />
    <div id="postItDiv" class="page">
        <?php
        $cxn = mysqli_connect('localhost', 'root', 'root', 'TimePost') or die(mysqli_error());
        $query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
        $result = mysqli_query($cxn, $query) or die(mysqli_error());

        while($row = mysqli_fetch_assoc($result)) {
            $post = $row['Post'];
            $time = $row['PostTime'];
            $idNo = $row['PostID'];
            $postColor = rand(1, 4);
            echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo . '<br />' . $post . ' at ' . $time . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo . '" /></form></div></div>';
        }

        if($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
            $postColors = rand(1, 4);
            $toPost = $_POST['textPost'];
            $date = date("y-m-d");
            $postTime = $_POST['display'];
            $postTime = floor($postTime);
            $insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
            $query3 = "SELECT PostID FROM tblTimePosts";

            $result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
            $result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
            if($result > 0) {
                while($row = mysqli_fetch_assoc($result3)) {
                    $idNo2 = $row['PostID'];
                }
                echo '<div id="post1"><p class="postParagraph">Post ID No.' . $idNo2 . '<br />' . $toPost . ' at ' . $postTime . ' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="' . $idNo2 . '" /></form></div></div>';
            }
            else
                echo "Add Failed";
        }

        /* if($_POST['chkDelete']) {
            for($i=0;$i<5;$i++){
                $del_id = $checkDelete[$i];
                $sql = "DELETE FROM $tblTimePosts WHERE PostID='$del_id'";
                $resulta = mysqli_query($cxn,$sql);
            }
        } */

        if($_POST['btnDelete']) { // from button name="delete"
            /* $checkbox = $_POST['checkbox']; //from name="checkbox[]"
            $countCheck = count($_POST['checkbox']);
            echo $countCheck;
            for($i=0;$i<$countCheck;$i++){
                $del_id = $checkbox[$i];
                $sql = "DELETE FROM tblTimePosts WHERE PostID=".$del_id."";
                $resulta = mysqli_query($cxn,$sql) or die(mysqli_error());
            } */

            $tbl_name = 'tblTimePosts';
            foreach($_POST['checkbox'] as $id => $value) {
                $sql = 'DELETE FROM `' . $tbl_name . '` WHERE `PostID`=' . (int) $id;
                mysqli_query($cxn, $sql);
            }
            header('Location: videoJudge.php');
        }
        ?>
    </div>
    <div id="videoContainer" class="page">
        <video id="video" poster="images/cover.png" controls>
            <source src="video/animationTest.m4v" />
        </video>
    </div>
    <div id="etc" class="page">
        <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
            <textarea name="textPost" id="textPost"> 
            </textarea>
            <input type="submit" name="btnPost" id="btnPost" value="Post" />
            <div id="tae">
                <input type="text" name="display" id="display" value="0" />
            </div>
        </form>
    </div>
    <div id="delButtonDiv">
        <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
            <input type="submit" value="Delete" name="btnDelete" />
        </form>
    </div>
</div>

这是我的代码,现在你的所有评论之后,但仍然无法正常工作。 :(

<?php
            $cxn = mysqli_connect('localhost','root','root','TimePost') or die(mysqli_error());
            $query = "SELECT PostID, ClientName, VideoName, PostTime, Post, Date FROM tblTimePosts";
            $result = mysqli_query($cxn,$query) or die(mysqli_error());
            while($row = mysqli_fetch_assoc($result)) {
                $post = $row['Post'];
                $time = $row['PostTime'];
                $idNo = $row['PostID'];
                $postColor = rand(1,4);
                echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo.'<br />'.$post.' at '.$time.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'" /></form>     
                </div></div>';
            }
             if ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['btnPost']) {
                $toPost = $_POST['textPost'];
                $date = date("y-m-d");
                $postTime = $_POST['display'];
                $postTime = floor($postTime);
                $insertPostQuery = "INSERT INTO tblTimePosts VALUES('','Mimagazine Asia','Chelsea','$postTime','$toPost','$date')";
                $query3 = "SELECT PostID FROM tblTimePosts";

                $result = mysqli_query($cxn, $insertPostQuery) or die(mysqli_error());
                $result3 = mysqli_query($cxn, $query3) or die(mysqli_error());
                if ($result > 0) {
                    while($row = mysqli_fetch_assoc($result3)) {
                        $idNo2 = $row['PostID'];
                    }
                    echo '<div id="post1"><p class="postParagraph">Post ID No.'.$idNo2.'<br />'.$toPost.' at '.$postTime.' seconds mark</p><div class="close"><form method="POST" action="videoJudge.php"><input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo2.'"
                    /></form></div></div>';
                }
                else
                    echo "Add Failed";
            }
            if($_POST['btnDelete'] == "Delete")
             {
                 $tbl_name = 'tblTimePosts';
                    foreach ($_POST['checkbox'] as $id => $value) 
                    {
                        $sql = 'DELETE FROM `'.$tbl_name.'` WHERE `PostID`='.(int)$id;
                        mysqli_query($cxn,$sql);
                    }
                    header('Location: videoJudge.php'); 
             }
        ?>
        </div>
        <div id="videoContainer" class="page">
            <video id="video" poster="images/cover.png" controls>
                <source src="video/animationTest.m4v" />
            </video>
        </div>
        <div id="etc" class="page">
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
                    <textarea name="textPost" id="textPost"> 
                    </textarea>
                        <input type="submit" name="btnPost" id="btnPost" value="Post" />
                        <div id="tae">
                            <input type="text" name="display" id="display" value="0" />
                        </div>

                <div id="delButtonDiv">
                <input type="submit" value="Delete" name="btnDelete" />
                </div>
            </form>
        </div>

3 个答案:

答案 0 :(得分:2)

这样做可以解决问题,请尝试:

<div id="etc" class="page">
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
        <textarea name="textPost" id="textPost"></textarea>
        <input type="submit" name="btnPost" id="btnPost" value="Post" />
        <div id="tae">
           <input type="text" name="display" id="display" value="0" />
        </div>

        <!-- END FORM REMOVED HERE -->

        <!-- FORM REMOVED HERE -->

     <div id="delButtonDiv"> <input type="submit" value="Delete" name="btnDelete" /> </div>
   </form>
</div>

[EDITED]

您的表单中有两个提交按钮,然后您需要知道您正在采取的操作,POST或DELETE,对吗?然后你可以在你自己的php脚本中为你的帖子创建一个基本的控制器,如下所示:

这是表格 - &gt; <form action="<?php $_SERVER['PHP_SELF']?>" method="POST">

以下是提交按钮

<input type="submit" name="btnPost" id="btnPost" value="Post" />
<input type="submit" value="Delete" name="btnDelete" />

$_SERVER['PHP_SELF']告诉您它会将表单数据发送给自己,如果是,则在脚本的开头执行类似的操作:

if ($_POST[btnPost] == "Post") 
// do some action to post the data

if ($_POST[btnDelete] == "Delete") 
// do some action to delete the data
现在明白了吗?

答案 1 :(得分:0)

您使用了两种表单,其中提交按钮属于第二种形式 当您按下提交按钮时,它只会提交一个空表格 将提交按钮移动到第一个表单应解决问题。

你有两行

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
..
...
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">

删除第二个,

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
                   ^ missing an echo

如果您想要提交回相同的网址,则无需设置表单操作。

答案 2 :(得分:0)

删除按钮的位置必须与您前往提交的相同表单中的复选框一起使用。

<form method="POST" action="videoJudge.php" >
                              ^______ url of the form you want to submit to. Possibly <?php echo $_SERVER['PHP_SELF']?>

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="'.$idNo.'" />
<input type="submit" value="Delete" name="btnDelete" /> 
</form>