0

我在删除表中的多条记录时遇到了一点问题。我使用复选框来删除它们,但它不起作用。我不知道它的确切代码是什么。

这是我的PHP代码

<?php   
    echo "<form action='#'>
        <fieldset>
            <input type='text' name='search' value='' id='searchtalents' placeholder='Search Keywords..' size='40'/>
        </fieldset> 
    </form>";

    echo "<form name='tmsform' method='POST' action=''>";

    $sql = "SELECT * FROM talentinfo WHERE 1 LIMIT 10";
    $result = mysql_query($sql);
    if (!$result) {
        echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
        exit;
    }

    if (mysql_num_rows($result) == 0) {
        echo "No rows found";
        exit;
    }

    echo"<div id='talentinfo'><table id='mr_database' name='myform' style='border:1px solid #fff;' cellspacing=0 cellpading='2' class='pretty'>
        <thead>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th>Mr Tracking Number</th>
                <th>First Name</th>
                <th>Middle Name</th>
                <th>Last Name</th>
                <th>Address</th>
                <th>Contact Number</th>
                <th>School</th>
                <th>Course</th>
                <th>Year Graduated</th>
                <th>Position Considered</th>
                <th>Referred Location</th>
                <th>Unit</th>
            </tr>
        </thead>";

    $counter = 40000;
    while ($row = mysql_fetch_assoc($result)) {
    $filled = (($counter % 2) == 1) ? "style='background-color:#BCD9E1;'" : "" ;
    $id = $row['talents_id'];

    echo "<tbody><tr {$filled} class='tmsdel'>";        
    echo "<td><a href ='#' rel='#edit_talents{$counter}'><center><img src='img/edit.gif' width='25' height='21' title='Edit'></center></a></td>";
    echo "<td><a href ='#'  id=".$row['talents_id'].'&idelete=talents'." class='delete'><center><img src='img/delete.png' width='25' height='21' title='Delete'></center></a></td>";
    echo "<td><input type='checkbox' name='checkbox[]' id='check".$row['talents_id']."' value='".$row['talents_id'].'&idelete=talents'."'/></td>";          
    echo "<td><a href='#' rel='#tracing_number{$counter}' style='text-decoration:none; font-weight:bold; color:#444;'>" . $row ['mr_tracking_number'] . "</a></td>";
    echo "<td>" . $row['firstname'] . "</td>";
    echo "<td>" . $row['middlename'] . "</td>";
    echo "<td>" . $row['lastname'] . "</td>";
    echo "<td>" . $row['address'] . "</td>";
    echo "<td>" . $row['contact_number'] . "</td>";
    echo "<td>" . $row['school'] . "</td>";
    echo "<td>" . $row['course'] . "</td>";
    echo "<td>" . $row['year_graduated'] . "</td>";
    echo "<td>" . $row['position_considered'] . "</td>";
    echo "<td>" . $row['referred_location'] . "</td>";
    echo "<td>" . $row['unit'] . "</td>";
    echo "</tr></tbody>";
?>

这是我的 Javascript

$(function(){
    $(document).ready(function(){
    });     

    $("#delete-all").click(function(){
        $("#mr_database").remove();
        $.ajax({
            url: "modules/delete-all.php",
            type: "get",
            async: false,
            data: "check"
        });
    });
});
4

2 回答 2

1

请按照以下步骤操作:

  1. 首先,您需要将任何类名应用于所有复选框。
  2. 按钮上的调用函数单击删除然后

      var allVals = '';
    
      $("#delete-all").click(function(){
    
       $('.checkboxclass :checked').each(function() {
         allVals = allVals + $(this).val() + ',';
       });
    }
    
  3. 然后你需要传入allVals变量ajax并发布到 .php 文件

    喜欢:数据:'ids=' + allVals 在 $.ajax 中

  4. 最后,您可以在 php 文件中获取此变量并对其执行删除过程。

    喜欢: $ids = explode(',', $_REQUEST['ids'); 并在 mysql 查询中使用 id

    希望这对您有所帮助。

于 2012-11-09T07:30:27.507 回答
0

相当简单的答案,因为我在这里看到的是一个用于查看表格并删除浏览器中的 html 而不是服务器上的数据的脚本。

您需要编写这里提到的 modules/delete-all.php 脚本: url: "modules/delete-all.php"

其中应包含“delete * FROM Talentinfo”SQL 查询。

但是,这将无缝删除所有表格内容。所以你可能想做一个提示:

$("#delete-all").click(function(){

Check = confirm("Do you really want to delete ALL DATA ? (Hint: there is no undo)");
if (Check == true) {
 $("#mr_database").remove();
    $.ajax({
        url: "modules/delete-all.php",
        type: "get",
        async: false,
        data: "check"
    });

}
});

delete_all.php 应该全部删除,所以只需:

<?php   

$sql = "delete * FROM talentinfo ";
$result = mysql_query($sql);
if (!$result) {
    echo "Could not successfully run query ({$sql}) from DB: " . mysql_error();
    exit;
} else {
    print "ok";
}
?>
于 2012-11-08T09:13:23.720 回答