简体   繁体   English

删除PHP中的选定记录

[英]Delete selected record in PHP

I am trying to deleted the selected record. 我正在尝试删除所选记录。

Here is my code: 这是我的代码:

echo '<td>';
echo $sqlUitlezenAccountsRank.'<br>';
echo '</td>';
echo '<td>';
echo '<form method="post">
    <input name="AccountEdit" value ="Bewerken" type="submit">
</form>'.'<br>';
echo '</td>';
echo '<td>';
echo '<form method="post">
<input name="AccountDelete" value ="Verwijderen" type="submit">
</form>'.'<br>';
echo '</td>';
echo '</tr>';

if (isset($_POST['AccountDelete'])) {
    $DeleteAccount = "DELETE FROM StaffLeden WHERE GebruikersID=".$sqlUitlezenAccountsEach['GebruikersID'];

if ($conn->query($DeleteAccount) === TRUE) {
    echo "Account succesvol verwijderd";
} else {
    echo "Fout bij het verwijderen van het account: " . $conn->error;
}

But if I click delete Verwijderen it deletes all data from the mysql table and it shows multiple times 'Account succesvol verwijderd' (Succesfully deleted the account) But I want that it only deletes the one where you click delete Verwijderen 但是,如果我单击删除Verwijderen它将删除mysql表中的所有数据,并多次显示'Account succesvol verwijderd' (成功删除了该帐户),但我希望它仅删除单击“删除Verwijderen

You can try by adding the id to the forms, check the hidden ids, and the the POST in the query 您可以尝试将ID添加到表单中,检查隐藏的ID,以及查询中的POST

                    echo '<td>';
                            echo $sqlUitlezenAccountsRank.'<br>';
                            echo '</td>';
                            echo '<td>';
                            echo '
                            <form method="post">
                                <input name="GebruikersID" value ="'.$sqlUitlezenAccountsEach['GebruikersID'].'" type="hidden">
                                <input name="AccountEdit" value ="Bewerken" type="submit">
                            </form>
                            '.'<br>';
                            echo '</td>';
                            echo '<td>';
                            echo '
                            <form method="post">
                                <input name="GebruikersID" value ="'.$sqlUitlezenAccountsEach['GebruikersID'].'" type="hidden">
                                <input name="AccountDelete" value ="Verwijderen" type="submit">
                            </form>
                            '.'<br>';
                            echo '</td>';
                            echo '</tr>';

                            if (isset($_POST['AccountDelete'])) {
                                $DeleteAccount = "DELETE FROM StaffLeden WHERE GebruikersID=".$_POST['GebruikersID'];

                                if ($conn->query($DeleteAccount) === TRUE) {
                                    echo "Account succesvol verwijderd";
                                } else {
                                    echo "Fout bij het verwijderen van het account: " . $conn->error;
                                }
                            }
                        }

1) You aren't allowing the page, when posting back, to specify the account to be deleted. 1)回发时,您不允许页面指定要删除的帐户。 You need a hidden field inside the "delete" form which holds the account ID. 您需要在“删除”表单内的一个隐藏字段,其中包含帐户ID。 Then you access this via the $_POST array and use it in your query (which, btw, should urgently be parameterised to better protect you from SQL injection attacks). 然后,您可以通过$ _POST数组来访问它,并在查询中使用它(顺便说一句,应该紧急地对其进行参数化,以更好地保护您免受SQL注入攻击)。

2) Clearly this code is inside some kind of loop. 2)显然,此代码位于某种循环内。 Therefore, your DELETE statement will run once every time the loop iterates, which presumably is once for every account in your database. 因此,您的DELETE语句将在每次循环迭代时运行一次,大概对数据库中的每个帐户都运行一次。 The code which renders the HTML (the loop) and the code which deals with postbacks should be in separate sections of the scripts. 呈现HTML(循环)的代码和处理回发的代码应在脚本的不同部分中。 They are intended to execute in separate processes (initial request to render HTML, then postback request to deal with the form submission) and should absolutely not be lumped in together. 它们旨在在单独的进程中执行(首先是呈现HTML的请求,然后是处理表单提交的回发请求),并且绝对不应将它们混为一谈。 In this case it has resulted in an entirely avoidable logic error. 在这种情况下,这将导致完全可以避免的逻辑错误。 In some more MVC- or API-oriented site designs, the two bits of code might even be in separate scripts altogether. 在一些更多的面向MVC或API的站点设计中,这两位代码甚至可能完全位于单独的脚本中。

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

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