2011-12-15 175 views
0

我的實現似乎不工作。你能指出什麼可能是問題或指向我更好的解決方案嗎?當我檢查複選框並點擊刪除按鈕時,它似乎沒有做任何事......請幫助我。PHP複選框多刪除

<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> 

回答

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> 

將帖子

有兩個表單中的提交按鈕後,那麼你需要知道你正在採取什麼行動,POST或刪除,對嗎?那麼您可以在自己的PHP腳本創建一個基本的控制器到您的文章,這樣的事情:

這裏是形式 - ><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 

明確了嗎?

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 

如果要提交回相同的URL,則不需要設置表單操作。

+0

你是什麼意思兩種形式?我應該在哪裏放置我的刪除代碼塊? – 2011-12-15 15:11:45

+0

什麼都沒有發生先生。仍然沒有任何反應:( – 2011-12-15 15:21:24

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>