E - Delete

Riverside High School stores information about its school clubs in a database. The school would like a Manage School Clubs webpage that allows an administrator to view all clubs currently stored in the database and remove clubs that are no longer running.

The webpage should automatically retrieve and display all records from the clubs table. Each club should have a radio button beside it, allowing the administrator to select one club. When Delete Selected Club is clicked, the form should use POST to send the selected club’s unique clubID back to the same webpage.

The PHP code should check whether a club has been selected. If it has, a DELETE query should remove the matching record from the database using its clubID. The webpage should then retrieve the records from the database again so that the updated list of clubs is displayed.

Requirements

  • Connect to the school database
  • Retrieve all records from the clubs table
  • Display the club name, day and teacher for each record
  • Generate a radio button for each club using its clubID as the value
  • Allow only one club to be selected at a time
  • Submit the form using POST
  • Have the form call the same PHP page
  • Check whether a clubID has been submitted
  • Delete the selected record using a SQL DELETE query
  • Display the updated list of clubs after the record has been delete
CREATE DATABASE school;

USE school;

CREATE TABLE clubs (
    clubID INT AUTO_INCREMENT PRIMARY KEY,
    clubName VARCHAR(50),
    day VARCHAR(20),
    teacher VARCHAR(50)
);

INSERT INTO clubs (clubName, day, teacher)
VALUES
('Coding Club', 'Monday', 'Mr Smith'),
('Chess Club', 'Tuesday', 'Mrs Brown'),
('Football Club', 'Wednesday', 'Mr Wilson'),
('Art Club', 'Thursday', 'Miss Campbell'),
('Gaming Club', 'Friday', 'Mr Jones');
if (isset($_POST["clubID"])) {

    $clubID = $_POST["clubID"];

    $sql = "DELETE FROM clubs
            WHERE clubID = $clubID";
}
<tr>
    <th>Select</th>
    <th>Club Name</th>
    <th>Day</th>
</tr>

<tr>
    <td>
        <input type="radio" name="clubID" value="1">
    </td>
    <td>Coding Club</td>
    <td>Monday</td>
</tr>