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

5

我想在每一行上添加一个删除按钮,这样当按下该按钮时,我就可以删除记录。我刚开始学习PHP、MySQL和Stack Overflow。

以下是从我的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

https://dev59.com/R6Dia4cB1Zd3GeqPILhI#FU8coYgBc1ULPQZFzipT - Matt
3个回答

8

只需按照以下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

在你的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";
}

1
非常感谢,这个方法很有效 :) 希望我的大学导师也能像这样回复!干杯 :) - Hamzah Amir
<?php //连接数据库和服务器的输入 $db = mysqli_connect("服务器", "用户名", "密码"); if(!$db) { echo "未连接到服务器"; } if (!mysqli_select_db($db,"用户名")) { echo "未选择数据库"; } require 'book.php'; //这是我们创建按钮的文件 $id = $_GET['id']; $sql_delete = "DELETE FROM Bookings WHERE Staff_ID = $id"; header ("location: book.php"); ?>@WebDev 我在这里做错了什么吗?对此很抱歉。 - Hamzah Amir
请查看更新的代码,您不需要要求 book.php @HamzahAmir - BetaDev
1
谢谢,问题解决了!:) - Hamzah Amir
1
这很简单但非常强大!! - Wachaga Mwaura
显示剩余2条评论

2
创建一个delete.php文件,接收$_GET['id'],然后运行sql删除该记录,当他们访问该页面时。有两种方式实现:一种是像下面我展示的那样使用锚标记;或者使用按钮代替锚点,并通过jquery运行ajax来发送id并运行上述提到的delete.php脚本。请保留HTML标签不做解释。
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

0
我建议您使用Ajax进行调用,类似于@webDev,但是不是调用PHP,而是使用AjaxCall调用JavaScript,然后使用JS隐藏/删除相关行,这样,用户就不必重新加载整个页面。
您可以使用此代码来补充选择为正确的答案,而不是href:
echo   '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>';

请在 <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>

2
是的,我知道,但是提问的用户是初学者,对于初学者来说,我们尽可能给出简单的答案更好。在学习过程中,最好先使用PHP来理解。 - BetaDev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接