【问题标题】:How to create a delete button for each row in the table?如何为表格中的每一行创建一个删除按钮?
【发布时间】:2021-09-09 08:56:15
【问题描述】:

我正在尝试编写一个 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>

【问题讨论】:

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

标签: php html mysql forms mysqli


【解决方案1】:

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

  1. 要删除一行,需要使用DELETEmysql 语句。 TRUNCATE 是错误的,因为它会从表中删除所有行。阅读official documentation了解更多信息。

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

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

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

总体而言,请参阅下面带有 cmets 的完整代码,以指导您了解为什么需要进行更改:

// 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>

【讨论】:

  • 非常感谢,它可以工作,但我仍在努力学习它是如何工作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2013-01-08
  • 2013-04-17
  • 1970-01-01
  • 2012-04-04
  • 2020-06-22
相关资源
最近更新 更多