我试图在每一行添加一个删除按钮,以便按下按钮时可以删除记录。我是 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
简单地使用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,
$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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)