2014-11-22 37 views
0

首先,我想道歉如果有任何重複的問題。PHP - 無法使用複選框刪除MySQL行(無形式)

正如標題所說,我試圖刪除用戶在jQuery幫助下檢查的多行。不幸的是,我沒有完成這項任務。

下面是我使用的代碼:==>

的index.php - >顯示在表格格式的數據..

<?php 
require_once '../../Classes/class.Validation.php'; 
$validate = new Validation('ecommerce'); 
$qu = "SELECT ProdCode, ProdName, ProdDescription, ProdRate, ProdTotalQuantity, ProdAvailability FROM products"; 
$validate->Query($qu); 
if ($validate->NumRows()) { 
?> 
    <div class="container-fluid"> 
     <div class="table-responsive"> 
      <table border="1" class="table table-bordered table-striped"> 
       <thead> 
        <tr> 
         <th><input type="checkbox" id="all" name="deletePrd[]"></th> 
         <th>Prod-Image</th> 
         <th>Prod-Code</th> 
         <th>Prod-Name</th> 
         <th>Prod-Description</th> 
         <th>Prod-Rate</th> 
         <th>Prod-Quantity</th> 
         <th>Prod-Availability</th> 
         <th>Action</th> 
        </tr> 
        <tr> 
         <th>&nbsp;</th> 
         <th>&nbsp;</th> 
        <th> 
         <input class="filter" name="Pro-Code" placeholder="Prod-Code" data-col="Prod-Code"> 
        </th> 
        <th> 
         <input class="filter" name="Prod-Name" placeholder="Prod-Name" data-col="Prod-Name"> 
        </th> 
        <th> 
         <input class="filter" name="Prod-Description" placeholder="Prod-Description" data-col="Prod-Description"> 
        </th> 
        <th> 
         <input class="filter" name="Prod-Rate" placeholder="Prod-Rate" data-col="Prod-Rate"> 
        </th> 
        <th> 
         <input class="filter" name="Prod-Total-Quantity" placeholder="Prod-Quantity" data-col="Prod-Quantity"> 
        </th> 
        <th> 
         <input class="filter" name="Prod-Availability" placeholder="Prod-Availability" data-col="Prod-Availability"> 
        </th> 
         <th>&nbsp;</th> 
        </tr> 
        </thead> 
        <tbody>     
         <?php 
         while ($row = $validate->FetchAllDatas()) { 
          echo '<tr> 
          <td><input type="checkbox" id="'.$row["ProdCode"].'" name="deletePrd[]"></td> 
          <td> 
           <img src="http://www.example.com/ECommerce/Images/Products/' 
           .$row["ProdCode"].'.jpg" alt="'.$row["ProdName"].'" 
           width="75" height="50" /> 
          </td>   
          <td>' . $row["ProdCode"] . '</td> 
          <td>' . $row["ProdName"] . '</td> 
          <td>' . $row["ProdDescription"] . '</td> 
          <td>' . $row["ProdRate"] . '</td> 
          <td>' . $row["ProdTotalQuantity"] . '</td> 
          <td>' . $row["ProdAvailability"] . '</td> 
          <td> 
           <a href="http://www.example.com/ECommerce/Administrators/Products/UpdateProduct.php?prd='.$row["ProdCode"].'">UPDATE</a><br /><a href="http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prd='.$row["ProdCode"].'">DELETE</a> 
          </td></tr>'; 
         } 
         ?>     
        </tbody> 
       </table> 
      </div> 
     </div> 
     <?php 
    } else { 
     echo '<p class="lead">No Products. Start inserting by clicking <a href="http://www.example.com/ECommerce/Administrators/Products/AddProducts.php">here</a></p>'; 
    } 
    ?> 

下面是我使用上點擊鏈接按鈕:==>

<a class="btn btn-link" id="del" name="DeleteLink">DELETE</a> 

這裏是jQuery代碼中刪除的產品:==>

$("a[id='del']").click(function() { 
     var count_checked = $("[name='deletePrd[]']:checked").length; 
     var checkedCode = $("[name='deletePrd[]']:checked").attr("id"); 
     if(count_checked == 0) { 
      alert("Please select product(s) to delete."); 
      return false; 
     } 
     if(count_checked == 1) { 
      //return confirm("Are you sure you want to delete this product ?"); 
      if (confirm('Are you sure you want to delete this product ?')) { 
       $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prdCode=" + checkedCode); 
      } 
     } else if(count_checked > 1) { 
      if (confirm('Are you sure you want to delete the selected products ?')) { 
       $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeletedSelectedProducts.php"); 
       for (var i = 0; i < count_checked; i++) { 
        console.log(i); 
       } 
      }   
     } 
    }); 

這裏的PHP代碼中刪除的產品:==>

DeletedSelectedProducts.php:

<?php 
session_start(); 

require_once '../Classes/class.Validation.php'; 
$validate = new Validation('developi_ecommerce'); 

if (isset($_POST['DeleteLink']) && isset($_POST['deletePrd'])) { 
    $delete = $_POST['deletePrd']; 

    $res = ""; 

    foreach ($delete as $code) { 
     $q = "DELETE FROM products WHERE ProdCode = '".$code."' "; 
     $res = $validate->Query($q); 
    } 
    if (!$res) { 
     echo '<br />Not Deleted'; 
    } else { 
     echo '<br />Deleted'; 
    } 
} else { 
    echo 'Product Not Set'; 
} 

if ($validate->IsConnected()) { 
    $validate->DisConnect(); 
} 

我試過了,我得到'Product not set'作爲輸出。

我很確定,我犯了一個錯誤,但我無法檢測到它。請幫助我,並嘗試幫助我糾正它。

謝謝您提前。

P.S.我已經嘗試並測試了單個產品刪除和所有產品刪除,並且它完美地工作,沒有任何錯誤。

更新1

下面是更新的jQuery與AJAX:==>

$("a[id='del']").click(function() { 
     var count_checked = $("[name='deletePrd[]']:checked").length; 
     var checkedCode = $("[name='deletePrd[]']:checked").attr("id"); 
     if(count_checked == 0) { 
      alert("Please select product(s) to delete."); 
      return false; 
     } 
     if(count_checked == 1) { 
      //return confirm("Are you sure you want to delete this product ?"); 
      if (confirm('Are you sure you want to delete this product ?')) { 
       $("a[id='del']").attr("href", "http://www.example.com/ECommerce/ActionFiles/DeleteProduct.php?prdCode=" + checkedCode); 
      } 
     } else if(count_checked > 1) { 
      if (confirm('Are you sure you want to delete the selected products ?')) { 
       /*$("a[id='del']").attr("href", "http://www.developigner.com/ECommerce/ActionFiles/DeletedSelectedProducts.php"); 
       for (var i = 0; i < count_checked; i++) { 
        console.log(i); 
       }*/ 
       $("a[id='del']"). 
       attr("href", "http://www.developigner.com/ECommerce/ActionFiles/DeletedSelectedProducts.php"). 
       ajax({ 
        type: "post", 
        data: count_checked.serialize(), 
        success: function() { 
         alert(count_checked); 
        } 
       }); 
      }   
     } 
    }); 
+0

沒有形式,無論是油潑st數據使用ajax onClick或通過逗號分隔的多個選定的ids作爲查詢參數在URL上 – GBD 2014-11-22 06:46:42

+0

我該怎麼做?事情是,我是一個初學者在這 – 2014-11-22 06:48:16

+0

請這樣:http://stackoverflow.com/questions/20543722/ajax-post-within-jquery-onclick – GBD 2014-11-22 06:52:27

回答

0

更換

data: count_checked.serialize(), 

隨着

data: $("[name='deletePrd[]']:checked").serialize(),