php帖子删除按钮

标签 php mysql button

嗨,我有一个 while 循环,可以打印出用户表中的所有用户名、名称等,但我需要的是一个按钮,可以运行一个查询,删除该行上的用户 ID,所以基本上在最后每个用户的行应该有一个删除按钮,仅删除该用户但不刷新页面。到目前为止我已经有了这个

while($row = mysql_fetch_array($theuser)){
    $accid = $row["account_id"];
    echo '<tr><td>'.$row["username"].'</td>
                        <td>'.$row["name"].'</td>
                        <td>'.$row["l_name"].'</td>
                        <td>'.$row["email"].'</td>
                        <td>'.$row["phone"].'</td>
                        <td><form action="" method="post" >
                        <input class="button" type="submit" name="submitdelete" value="delete"></form></td></tr>';}

但我遇到的问题是,如果我输入这行代码:

if(isset($_POST['submitdelete'])){
    mysql_query("Delete from users where account_id='$accid'");
}

在 while 循环内,它将删除所有用户。如果我把它放在 while 循环之外,它就不会获得 id。

有没有一种方法可以编写输入的名称,例如: name="submitdelete34"和 34 我的意思是用户的 id,所以在 while 循环中它的编码如下:

<input class="button" type="submit" name="submitdelete'.$accid.'" value="delete"></form>'

并使其发挥作用。

请帮助我

最佳答案

让我们变得更容易。让我们添加一个隐藏的输入:

<input type="hidden" name="account_id" value="<?php echo $accid;?>">
<input class="button" type="submit" name="submitdelete" value="delete">

现在在你的 php 中你可以获得 id:

$connection = mysqli_connect("my_host", "my_user", "my_password", "my_db");

// simple error handler
if (!$connection){
    echo "SQL connection failed: ". mysqli_error($connection);
    exit;
}

if(isset($_POST['submitdelete'])){
    $acctid = (int) $_POST['account_id'];// <-- gotta protect from sql injection.
    mysqli_query($connection, "Delete from users where account_id='$accid'");
}

但是:

更好的解决方案是不删除该记录。表格上有一个可以称为“事件”的字段,您可以将其设置为 TINYINT。通过这种方式删除记录,您可以设置active=0

如果您误删除了,您可以随时将其恢复为 active=1

请注意,我使用的是 mysqli_ 而不是 mysql_。您需要将整个代码更新为 mysqli_ 因为 the other is deprecated 。它已从较新版本的 php 中删除

关于php帖子删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905490/

相关文章:

php - yii 框架 php 中的条件条件

php - 检查MySQL字段值是否等于某个数字

mysql - MySQL无法添加外键?

python - 对某个对象进行投票最有效的方法是什么?

python - TkInter:销毁多个按钮,然后重新创建多个(可能不同)按钮

html - 如何创建带有图像的按钮?

javascript - 卸载页面时 chrome 无法按预期工作

php - 抑制 “Ambiguous class resolution” 警告

安卓工作室 : error: type annotations are not supported

php - 如何从 mysql 数据库获取 Json 形式的特定元素