1

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)

1
  • If you're up to using checkboxes, I refer people to this Q&A on SO, which is what I used; it worked for me. Plus, you can change the checkbox to radio; which is what I did. Commented Mar 20, 2014 at 16:18

2 Answers 2

3

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,

Sign up to request clarification or add additional context in comments.

7 Comments

This method is open to SQL injection.
@Fred-ii- yes i know, i mention to do some security stuff :) however i will update it
Actually, that can also be written as $recored_id = mysqli_real_escape_string($con,$_POST['recored_id']);
@Fred-ii- you are right, but we expect to have integer value in $_POST['recored_id']. so if it's not the query will fill, and this is what we want. however all ways lead to Rome
^--« Translated "Rome is a beautiful city!" ;-) - Glad to see the OP accepted your answer. +1 ;-)
|
0

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

Comments

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.