Nick Gilbert
Nick Gilbert

Reputation: 4240

Delete MySQL entry from HTML page

I'm trying to design a control page for my website that allows admins to delete requests to use a 3D printer at my school. The page displays a query of the schedule from the MySQL database and puts a delete button next to every entry. However, I would like to link the information in each entry to its respective delete button and have no idea how to do that. You can see the page itself here: http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php and this is my code for displaying the information

if ($result = $mysqli->query($query)) {
    // see if any rows were returned
    if ($result->num_rows > 0) {
        // yes
        // print them one after another
        echo "<table cellpadding=10 border=1>";
        echo "<tr>";
        echo "<th>Control</th>";
        echo "<th>Student ID</th>";
        echo "<th>Last Name</th>";
        echo "<th>First Name</th>";
        echo "<th>Date and Time</th>";
        echo "<th>Description</th>";
        echo "</tr>";
        $key = 1;
        while($row = $result->fetch_array()) {
            echo '<form method="POST" name="deleterequest" action = "delete request.php">';
            echo "<tr>";
            echo "<td><input name='".$row[5]."'type='submit' value='Delete' ></td>";
            echo "<td>".$row[0]."</td>";
            echo "<td>".$row[1]."</td>";
            echo "<td>".$row[2]."</td>";
            echo "<td>".$row[3]."</td>";
            echo "<td>".$row[4]."</td>";
            echo "</tr>";
            echo "</form>";
        }
        echo "</table>";
    }
    else {
        // no
        // print status message
        echo "No upcoming prints found!";
    }

    // free result set memory
    $result->close();
}

I can't figure out how to link the information in a row with the delete button that I put in the row to send to the delete request.php program (which I have no code for yet)

Upvotes: 1

Views: 140

Answers (2)

Mohammad Ahmad
Mohammad Ahmad

Reputation: 745

you should put hidden field that hold each record id.

As below

while($row = $result->fetch_array()) {
            echo "<tr>";
            echo "<td>";
            echo '<form method="POST" name="deleterequest" action = "delete request.php">';
            echo "<input name='recored_id' type='hidden' value='".$row['id']."' >";
            echo "<input name='delete'type='submit' value='Delete' >";
            echo "</form>";
            echo "</td>";
            echo "<td>".$row[0]."</td>";
            echo "<td>".$row[1]."</td>";
            echo "<td>".$row[2]."</td>";
            echo "<td>".$row[3]."</td>";
            echo "<td>".$row[4]."</td>";
            echo "</tr>";
            echo "</form>";
        }

Now from your request.php page you should do something like this

$recored_id = (int) $_POST['recored_id'];
$sql = "DELETE FROM table_name WHERE recored_id='$recored_id'";

Don't forget to give this code some security validation

all the best,

Upvotes: 3

echolocation
echolocation

Reputation: 1120

You're going to need some hidden inputs, like so:

while($row = $result->fetch_array()) {

    echo "<tr>";
    echo '<td><form method="POST" name="deleterequest" action = "deleterequest.php">';
    echo "<input type='hidden' name='val0' value='" . $row[0] . "'>";
    echo "<input type='hidden' name='val1' value='" . $row[1] . "'>";
    echo "<input type='hidden' name='val2' value='" . $row[2] . "'>";
    echo "<input type='hidden' name='val3' value='" . $row[3] . "'>";
    echo "<input type='hidden' name='val4' value='" . $row[4] . "'>";
    echo "<input name='".$row[5]."'type='submit' value='Delete' >";
    echo "</form></td>";
    echo "<td>".$row[0]."</td>";
    echo "<td>".$row[1]."</td>";
    echo "<td>".$row[2]."</td>";
    echo "<td>".$row[3]."</td>";
    echo "<td>".$row[4]."</td>";
    echo "</tr>";
}

You should change the name of these inputs though

Upvotes: 0

Related Questions