[英]Adding a delete button in PHP on each row of a MySQL table
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
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.php
file, 在您的
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";
}
Create a delete.php file that receives a $_GET['id'], then runs sql to delete that record when they go to that page. 创建一个delete.php文件,该文件接收一个$ _GET ['id'],然后在他们转到该页面时运行sql删除该记录。 Done via two ways: an anchor tag like I've shown below,
通过两种方式完成:如下所示的锚标记,
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
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.