Estoy tratando de agregar un botón de eliminación en cada fila para poder eliminar un registro cuando se presiona el botón. Soy nuevo en PHP y MySQL y Stack Overflow.
A continuación se muestra mi tabla que extrae información de mi base de datos MySQL y eso funciona.
<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 loopSimplemente usando PHP de la siguiente manera (Puedes usar 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 En su archivo 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"; }Cree un archivo delete.php que reciba un $_GET['id'], luego ejecute sql para eliminar ese registro cuando vayan a esa página. Hecho de dos maneras: una etiqueta de anclaje como la que se muestra a continuación,
O haga un botón en lugar de un ancla que ejecute ajax (a través de jquery) enviando esa identificación y ejecutando el script delete.php que mencioné anteriormente.
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 loopLe recomendaría usar Ajax para hacer una llamada, algo así como @webDev pero en lugar de llamar a PHP, llame a Javascript con AjaxCall y luego use JS para ocultar/eliminar la fila en cuestión, de esa manera, el usuario no No tienes que recargar toda la página.
Puedes complementar la respuesta que selecciones como correcta con este código en lugar del href:
echo '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>'; Y agregue la siguiente función en la sección <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>