Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

887
Views
Agregar un botón de eliminación en PHP en cada fila de una tabla MySQL

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 loop
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Simplemente 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"; }
over 4 years ago · Santiago Trujillo Report

0

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 loop
over 4 years ago · Santiago Trujillo Report

0

Le 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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!