5

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.

Below is my table which extract information from my MySQL database and that works.

       <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
halfer
  • 19,824
  • 17
  • 99
  • 186
Hamzah Amir
  • 63
  • 1
  • 1
  • 6
  • http://stackoverflow.com/questions/43281598/how-to-delete-a-specific-row-in-a-table-using-javascript#comment73631786_43281598 – Matt Apr 07 '17 at 20:13

3 Answers3

8

Simply using PHP as follows (You can use 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";
}
BetaDev
  • 4,516
  • 3
  • 21
  • 47
2

Create a delete.php file that receives a $_GET['id'], then runs sql to delete that record when they go to that page. 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.

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
BetaDev
  • 4,516
  • 3
  • 21
  • 47
clearshot66
  • 2,292
  • 1
  • 8
  • 17
0

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.

You can complement the answer you select as correct with this code instead of the href:

echo   '<td><button onclick="deleteRow('.$book['Staff_ID'].')">Delete</button></td>';

And add the following function in the <head> section:

<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>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 2
    Yes I know, but the user who posted the question is beginner, and for beginner its better to have simple answer as much as we can. While learning its better to understand using PHP first – BetaDev Apr 07 '17 at 20:24