如何为表格中的每一行创建一个删除按钮?

How to create a delete button for each row in the table?

提问人:ubuntu-user 提问时间:6/26/2021 最后编辑:ubuntu-user 更新时间:6/26/2021 访问量:1094

问:

我正在尝试编写一个PHP代码,该代码从mySQL连接并获取数据单元格,并为每一行创建一个删除按钮,因为它像在mySQL数据库中一样增加。

请帮我弄清楚我做错了什么?!!

[我的表名是“VF”,其结构是这样的]

ID     Password1     Password2

[我使用的PHP变量是]

$Connect=mysqli_connect($host,$user,$Pass,$db);
$QueryVF = mysqli_query($Connect,'SELECT * from vf');
$ID =$_POST['ID'];
$DelVF = mysqli_query($Connect,'DELETE from vf where ID =$ID');

/* ALSO TRIED This:  $DelVF = mysqli_query($Connect,'TRUNCATE vf'); */

[我的HTML和PHP代码]

       
<html>
    <body>             
        <form method="POST">
            <table border="1" class=".input">              <!--vf Table-->
                        <tr>
                                <th>ID</th>
                                <th>Password 1</th>
                                <th>Password 2</th>
                                <th>Drop data</th>
                        </tr>
                            <?php
                                while ( $row = mysqli_fetch_array($QueryVF)){
                                echo'<tr>';
                                    echo'<td>'.$row['ID'].'</td>';
                                    echo'<td>'.$row['Password1'].'</td>';
                                    echo'<td>'.$row['Password2'].'</td>';
                                    echo "<td><button type=\"submit\" name=\"Delete\" oneclick=\"$DelVF\">Drop Data</button></td>";
                                        if($_POST['Delete']){
                                            while ($DelVF = mysqli_query($Connect,"'DELETE from vf where ID =$ID'") )
                                            $DelVF;
                                            echo "VF table's row is successfully deleted";
                                                                }
                                    echo'</tr>';
                              }
                             ?>
                        </table>
        </form>
    </body>
</html>

php html mysql 表单 mysqli

评论

2赞 ADyson 6/26/2021
phpMyAdmin 不是数据库。您从 mySQL 获取数据,而不是 phpMyAdmin。phpMyAdmin只是一个应用程序(几个可用应用程序之一),可用于管理mySQL服务器。
1赞 ADyson 6/26/2021
无论如何,您的代码似乎尝试删除整个表,而不是特定行。此外,它可能会运行多次,而不是一次,因为您正在循环它。将处理删除的代码移到循环之外,并确保表单提交要删除的行的 ID,以便可以使用合适的 WHERE 子句编写 DELETE 查询,并将行 ID 作为参数传递。可以肯定的是,如果您搜索,您已经可以在网上找到很多基本概念的示例,这是一个常见的要求
0赞 ADyson 6/26/2021
P.p.s 请在使用前阅读标签说明 - “处理”标签是指特定工具,而不是一般处理。我将为您删除它和 phpMyAdmin 标签。也请参观,以便您充分了解该网站的工作原理 - stackoverflow.com/tour
0赞 Valerio Bozz 6/26/2021
顺便说一句,您的代码容易受到 SQL 注入的影响,因为用户变量未正确转义。切勿在生产环境中测试代码。阅读更多:php.net/manual/en/security.database.sql-injection.php

答:

2赞 Andrea Olivato 6/26/2021 #1

正如@ADyson在他们的评论中分享的那样,除了 和 之间的混淆之外,还有一些事情需要解决。mysqlphpmyadmin

  1. 要删除一行,您需要使用 mysql 语句。 是错误的,因为它会从表中删除所有行。有关详细信息,请阅读官方文档DELETETRUNCATE

  2. 将数据管理移到循环之外,否则将多次运行该代码POSTwhile

  3. 您不会以任何方式传递 ID。一个简单的方法是将属性添加到按钮,如下所示:POSTvaluesubmit

echo '<button type="submit" name="Delete" value="'.$row['ID'].'">Drop Data</button>';

总体而言,请参阅下面的完整代码和注释,以指导您理解为什么需要更改:

// Manage the POST data at the top of your file
if (isset($_POST['Delete'])) {
    // If you receive the Delete post data, delete it from your table
    $delete = 'DELETE FROM vf WHERE ID = ?';
    $stmt = $Connect->prepare($delete);
    $stmt->bind_param("i", $_POST['Delete']);
    $stmt->execute();
}
// Run your select AFTER the DELETE, so that you will get the updated table
$QueryVF = mysqli_query($Connect, 'SELECT * from vf');

?>

<html>
    <body>             
        <form method="POST">
            <table border="1">              
                <tr>
                        <th>ID</th>
                        <th>Password 1</th>
                        <th>Password 2</th>
                        <th>Drop data</th>
                </tr>
                    <?php
                    while ($row = mysqli_fetch_array($QueryVF)) {
                        echo'<tr>';
                        echo'<td>'.$row['ID'].'</td>';
                        echo'<td>'.$row['Password1'].'</td>';
                        echo'<td>'.$row['Password2'].'</td>';
                        // Add the ID to the value attribute so that it gets passed via POST
                        echo '<td><button type="submit" name="Delete" value="'.$row['ID'].'">Drop Data</button></td>';
                        echo'</tr>';
                    }
                    ?>
            </table>
        </form>
    </body>
</html>

评论

0赞 ubuntu-user 6/26/2021
非常感谢,它有效,但我仍在努力了解它是如何工作的。
0赞 Sadikul Haque Sadi 3/20/2022
它工作得很好。但是也有效。mysqli_query($Connect, "delete from vf where ID='$_POST[Delete]'") or die(mysqli_error($Connect));
1赞 Andrea Olivato 3/21/2022
@SadikulHaqueSadi不,它没有。该代码使应用程序对 SQL 注入广泛开放。在这里阅读