从HTML页面删除MySQL条目

时间:2014-03-20 16:00:36

标签: php html mysql database

我试图为我的网站设计一个控制页面,允许管理员删除我学校使用3D打印机的请求。该页面显示来自MySQL数据库的计划查询,并在每个条目旁边放置一个删除按钮。但是,我想将每个条目中的信息链接到其各自的删除按钮,并且不知道如何执行此操作。您可以在此处查看页面本身:http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php这是我显示信息的代码

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();
}

我无法弄清楚如何将行中的信息与我放入行中的删除按钮链接以发送到delete request.php程序(我还没有代码)

2 个答案:

答案 0 :(得分:3)

你应该把隐藏的字段保存每个记录ID。

如下所示

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>";
        }

现在,从您的request.php页面,你应该做这样的事情

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

不要忘记为此代码提供一些安全性验证

一切顺利,

答案 1 :(得分:0)

您需要一些隐藏的输入,例如:

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>";
}

您应该通过

更改这些输入的名称