PHP - 无法使用复选框删除MySQL行(没有表单)

时间:2014-11-22 06:35:52

标签: php jquery 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>';
    }
    ?>

这是我点击按钮时使用的链接:==&gt;

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

这是删除产品的jQuery代码:==&gt;

$("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代码:==&gt;

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

这是使用AJAX更新的jQuery:==&gt;

$("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);
                    }
                });
            }           
        }
    });

1 个答案:

答案 0 :(得分:0)

替换

data: count_checked.serialize(),

使用

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