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
1

3 Answers 3

9

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";
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much, this has worked :) Wish mu university tutors respond like this! cheers:)
<?php //Connection and input to the database and server $db = mysqli_connect("server", "usernname", "password"); if(!$db) { echo "not connected to server"; } if (!mysqli_select_db($db,"username")) { echo "database not selected"; } require 'book.php'; //this is the file where we created the button $id = $_GET['id']; $sql_delete = "DELETE FROM Bookings WHERE Staff_ID = $id"; header ("location: book.php"); ?> @webDev am I doing something wrong here? sorry about this
see updated code and you dont need to require book.php @HamzahAmir
THANK YOU AGIAN SOLVED IT!:)
This is simple but powerful!!
|
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

Comments

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>

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.