简体   繁体   English

从表格中删除-通过表格

[英]Delete From Table - Via Form

I have a HTML table that displays all my table entries. 我有一个显示所有表条目的HTML表。 Also in the table is a delete button next to every SQL entry. 该表中的每个SQL条目旁边还有一个删除按钮。

I want to be able to delete an entry the user selects. 我希望能够删除用户选择的条目。 I've made the form to Post PHP_Self , and I'm passing in the index $i from the while loop as a reference: 我已经将表单制作为Post PHP_Self ,并且我将while循环中的索引$i作为参考传递:

$i = 0;

while($row = mysqli_fetch_array($result)){

    ?>

    <tr>
        <td>
            <? echo $row['uniqueIdentifier']; ?>
        </td>

        <td>
            <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
                <input type='hidden' name='remove_entrie_ID' value='<? echo $i; ?>' />
                <input type='submit' value='Delete' name='remove_entrie'>
            </form>
        </td>

    </tr>

    <?

    $i++;

}

So this is passed to itself, I now want to do a DELETE WHERE 'INDEX OF TABLE' == $i , type thing? 所以这是传递给它自己,我现在想在DELETE WHERE 'INDEX OF TABLE' == $i ,输入东西吗? I don't even know if this is possible. 我什至不知道这是否可能。

if(isset($_POST['remove_entrie'])){

    $index = $_POST['remove_entrie_ID']
    echo "Index: " . $index;

    //mysqli_query($con, "DELETE FROM Users WHERE INDEX = '$index'");
}

I'm basically using the $i to pick out the index of how the table was loaded, and then using that to pick out which row I want to delete. 我基本上是使用$ i来选择如何加载表的索引,然后使用它来选择要删除的行。 But I have a feeling this can't be done? 但是我有种感觉无法完成?

I basically want to delete a row the user has selected from the table. 我基本上想删除用户从表中选择的行。

You don't need $i variable. 您不需要$ i变量。 Just make simple modification in your list: 只需在列表中进行简单的修改即可:

while($row = mysqli_fetch_array($result)){
?>
<tr>
    <td>
        <? echo $row['uniqueIdentifier']; ?>
    </td>

    <td>
        <form action="" method="post">
            <input type="hidden" name="remove_entrie_ID" value="<? echo $row['uniqueIdentifier']; ?>" />
            <input type="submit" value="Delete" name="remove_entrie">
        </form>
    </td>

</tr>

<?

} }

It is also good idea to escape specials chars etc in your post variable to avoid sql injection. 最好在post变量中转义特殊字符等,以避免sql注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM