-1

我正在建立一个我之前提出并解决的问题:前端 mysql,删除一行

基本上,我有一个前端,用户可以在其中查看数据库。我不想在每一行旁边都有一个删除按钮,而是有一个可以为多行选择的复选框。然后,用户只需单击一个删除按钮,所有选定的行都会被删除。我根本不知道多少 php 和 mysql,所以我不确定如何处理我已经拥有的代码。

目前,onclick 调用删除函数。任何人都可以帮忙吗?

我有一个 php 文件,它将 mysql 数据的 html 输出为 long strong,我需要更改的部分是:

$display_string .= "<td class='blank'><input type=\"button\" VALUE=\"Delete\" onclick='delFunction(" . $row['ID'] . ")' ></td>";

接下来我的删除功能:

function delFunction(ID){
    // confirm delete
    if (!confirm(\"Are you sure you want to delete?\")) return false;

    var ajaxRequest;  // The variable that makes Ajax possible!


    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");
            } catch (e){
                // Something went wrong
                alert(\"Your browser broke!\");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('ajaxDiv');
            ajaxDisplay.innerHTML = ajaxRequest.responseText;
        }
    }

    var queryString = \"?ID=\" + ID


    ajaxRequest.open(\"GET\", \"delete_row.php\" + queryString, true);
    ajaxRequest.send(null); 
}
4

1 回答 1

0

据我了解您的问题,我发布了一些前端和后端的代码

前端示例代码

<body>
<form action="delete.php" method="post">
<input type="checkbox" name="del_chk[]">Item1
<input type="checkbox" name="del_chk[]">Item2
<input type="checkbox" name="del_chk[]">Item3
<input type="checkbox" name="del_chk[]">Item4
.
.
<input type="submit">
</form>

......

您的后端代码现在将是...

<?php
if(isset($_POST['del_chk'])){
$chkbox=$_POST['del_chk'];
foreach($chkbox as $key=>$value) {

//Now you can get your value and use it in mysql delete statements


//for example
$del_query="DELETE FROM `yourtable` WHERE `pri_key`=$value;";
if(mysql_query($del_query)) {
echo "Successful Deletion of: ".$value;
}
else 
{
echo "Unsuccessful Deletion of: ".$value;
} 

} //end foreach
}
?>

我对ajax了解不多。但是你可以使用ajax来调用这个页面..

于 2012-06-20T02:04:11.997 回答