在 PHP 中在 MySQL 表的每一行上添加一个删除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43286387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:48:33  来源:igfitidea点击:

Adding a delete button in PHP on each row of a MySQL table

phphtmlmysql

提问by Hamzah Amir

I am trying to add a delete button on each row so that I can delete a record when the button is pressed. I am new to PHP and MySQL and Stack Overflow.

我试图在每一行上添加一个删除按钮,以便在按下该按钮时可以删除一条记录。我是 PHP 和 MySQL 以及 Stack Overflow 的新手。

Below is my table which extract information from my MySQL database and that works.

下面是我的表格,它从我的 MySQL 数据库中提取信息并且有效。

       <table class="table" >
       <tr>
       <th> Staff ID </th>
       <th> Staff Name </th>
       <th> Class </th>
       <th> Action </th>

       </tr>   

       <?php

       while($book = mysqli_fetch_assoc($records)){

       echo "<tr>";
       echo "<td>".$book['Staff_ID']."</td>";
       echo "<td>".$book['Staff_Name']."</td>";
       echo "<td>".$book['Class']."</td>";
       echo "</tr>";
       }// end while loop

回答by webDev

Simply using PHP as follows (You can use JS)

简单的使用PHP如下(可以使用JS)

while($book = mysqli_fetch_assoc($records)){

echo "<tr>";
echo "<td>".$book['Staff_ID']."</td>";
echo "<td>".$book['Staff_Name']."</td>";
echo "<td>".$book['Class']."</td>";
echo "<td><a href='delete.php?id=".$book['Staff_ID']."'></a></td>"; //if you want to delete based on staff_id
echo "</tr>";
}// end while loop

In your delete.phpfile,

在您的delete.php文件中,

$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from you table
//query : delete where Staff_id = $id
// on success delete : redirect the page to original page using header() method
$dbname = "your_dbname";
$conn = mysqli_connect("localhost", "usernname", "password", $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM Bookings WHERE Staff_ID = $id"; 

if (mysqli_query($conn, $sql)) {
    mysqli_close($conn);
    header('Location: book.php'); //If book.php is your main page where you list your all records
    exit;
} else {
    echo "Error deleting record";
}

回答by clearshot66

Create a delete.php file that receives a $_GET['id'], then runs sql to delete that record when they go to that page. Done via two ways: an anchor tag like I've shown below,

创建一个接收 $_GET['id'] 的 delete.php 文件,然后运行 ​​sql 以在他们转到该页面时删除该记录。通过两种方式完成:如下所示的锚标记,

Or make a button instead of an anchor runs ajax (through jquery) sending that id and running the the delete.php script from above I mentioned.

或者制作一个按钮而不是锚点运行ajax(通过jquery)发送该id并运行我上面提到的delete.php脚本。

table class="table" >
       <tr>
       <th> Staff ID </th>
       <th> Staff Name </th>
       <th> Class </th>
       <th> Action </th>

       </tr>   

       <?php

       while($book = mysqli_fetch_assoc($records)){

       echo "<tr>";
       echo "<td>".$book['Staff_ID']."</td>";
       echo "<td>".$book['Staff_Name']."</td>";
       echo "<td>".$book['Class']."</td>";
       echo "<td><a href='delete.php?id=".$book['Staff_ID']."'>Delete</a></td>";
       echo "</tr>";
       }// end while loop

回答by Ricardo Ortega Maga?a

I would recommend you to use Ajax to make a call, something like @webDev but instead of calling the PHP, call a Javascript with an AjaxCall, and then, use JS to hide/delete the row in question, that way, the user didn't have to reload the whole page.

我建议您使用 Ajax 进行调用,例如 @webDev 但不是调用 PHP,而是使用 AjaxCall 调用 Javascript,然后使用 JS 来隐藏/删除有问题的行,这样,用户就没有不必重新加载整个页面。

You can complement the answer you select as correct with this code instead of the href:

您可以使用此代码而不是 href 来补充您选择的正确答案:

echo   '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>';

And add the following function in the <head>section:

并在该<head>部分添加以下功能:

<script>
    function deleteRow(StaffID){
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {                     

        if (xhttp.readyState == 4 && xhttp.status == 200) {
                  alert("Deleted!");
            }
        };
        document.getElementById("table").deleteRow(x);
        xhttp.open("GET", "delete.php", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("id="+StaffID);
        }       
</script>