php如何用html表按钮从数据库中删除特定行

jmp7cifd  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(267)

我正试图用一个按钮从数据库中删除一个特定的条目。我知道这个问题已经被问过好几次了,不幸的是,这些解决方案对我来说并不奏效。如果我点击第三行的按钮,目标是删除这一行。我有一个问题,我总是只删除最后一个ID或一次删除所有ID。
也许有人能帮我,谢谢。
管理.php

<?php
include('connection.php');
include('read.php');
?>

<form action="admin.php" method="post">
            <div class="table-wrapper">
                <div class="table-scroll">
                    <table id="myTable">
                        <tr>
                            <th>ID</th>
                            <th>Kartentyp</th>
                            <th>Absender</th>
                            <th>Empfänger</th>
                            <th>Sendedatum</th>
                            <th id="smallCol">Verschickt</th>
                            <th id="smallCol">Bestätigung</th>
                            <th>Edit</th>
                        </tr>

                        <?php
                        foreach ($result as $row) {
                            if ($row['Dispatched'] == 0) {
                                $dispatched = 'Pending';
                            } else {
                                $dispatched = 'Versendet';
                            }
                            ?>
                            <tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
                                <td><?php echo $row['ID']; ?></td>
                                <td><?php echo $row['Category']; ?></td>
                                <td><?php echo $row['Sender']; ?></td>
                                <td><?php echo $row['Receiver']; ?></td>
                                <td><?php echo $row['SendDate']; ?></td>
                                <td><?php echo $dispatched; ?></td>
                                <td>Placeholder</td>
                                <td><input type="submit" name="delete" value="delete" >
                                    <button data-target="modal1" class="modal-trigger">Modal</button>
                                </td>
                            </tr>
                            <?php

                        }
                        if (isset($_POST['delete'])) {
                            echo $row['ID'];

                            $deleteQuery = "DELETE FROM card WHERE id = " . $row['ID'];
                            $statement = $pdo->prepare($deleteQuery);
                            $statement->execute();
                        }
                        ?>
                    </table>
                </div>
            </div>
        </form>

阅读.php

<?php
include('connection.php');

$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
    foreach ($statement->fetchAll() as $row) {
        $id = $row['ID'];
        $imagePath = $row["ImagePath"];
        $sender = $row["Sender"];
        $senderEmail = $row["SenderEmail"];
        $receiver = $row["Receiver"];
        $receiverEmail = $row["ReceiverEmail"];
        $subject = $row["Subject"];
        $text = $row["Text"];
        $sendDate = $row["SendDate"];
        $dispatched = $row["Dispatched"];
        $category = $row['Category'];

    }
}
?>
luaexgnf

luaexgnf1#

您可以这样修改代码:

<?php
include('connection.php');
include('read.php');

if( !empty($_POST) ){

    foreach( $_POST as $key_post => $value_post ){

        if (preg_match('/delete_/i',$key_post) ) {

            $id_to_delete = (integer) str_replace('delete_','', $key_post);

            $deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
            $statement = $pdo->prepare($deleteQuery);
            $statement->execute();
        }
    }
}   

?>

<form action="admin.php" method="post">
            <div class="table-wrapper">
                <div class="table-scroll">
                    <table id="myTable">
                        <tr>
                            <th>ID</th>
                            <th>Kartentyp</th>
                            <th>Absender</th>
                            <th>Empfänger</th>
                            <th>Sendedatum</th>
                            <th id="smallCol">Verschickt</th>
                            <th id="smallCol">Bestätigung</th>
                            <th>Edit</th>
                        </tr>

                        <?php
                        foreach ($result as $row) {
                            if ($row['Dispatched'] == 0) {
                                $dispatched = 'Pending';
                            } else {
                                $dispatched = 'Versendet';
                            }
                            ?>
                            <tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
                                <td><?php echo $row['ID']; ?></td>
                                <td><?php echo $row['Category']; ?></td>
                                <td><?php echo $row['Sender']; ?></td>
                                <td><?php echo $row['Receiver']; ?></td>
                                <td><?php echo $row['SendDate']; ?></td>
                                <td><?php echo $dispatched; ?></td>
                                <td>Placeholder</td>
                                <td><input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
                                    <button data-target="modal1" class="modal-trigger">Modal</button>
                                </td>
                            </tr>
                            <?php

                        }
                        ?>
                    </table>
                </div>
            </div>
        </form>

您的错误是没有使用post值删除行,而是删除了来自查询读取的row变量中的最后一个id存储。
因为你在pdo中错误地使用了prepare函数。

y0u0uwnf

y0u0uwnf2#

您可以通过使用get请求将其存档,而无需将整个数据发布到服务器。

if( !empty($_GET['id']) ){
          $deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
          $statement = $pdo->prepare($deleteQuery);
          $statement->execute();
          header('location:youfilename.php');
          exit;

    }   

?>
                <div class="table-wrapper">
                    <div class="table-scroll">
                        <table id="myTable">
                            <tr>
                                <th>ID</th>
                                <th>Kartentyp</th>
                                <th>Absender</th>
                                <th>Empfänger</th>
                                <th>Sendedatum</th>
                                <th id="smallCol">Verschickt</th>
                                <th id="smallCol">Bestätigung</th>
                                <th>Edit</th>
                            </tr>

                            <?php
                            foreach ($result as $row) {
                                if ($row['Dispatched'] == 0) {
                                    $dispatched = 'Pending';
                                } else {
                                    $dispatched = 'Versendet';
                                }
                                ?>
                                <tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
                                    <td><?php echo $row['ID']; ?></td>
                                    <td><?php echo $row['Category']; ?></td>
                                    <td><?php echo $row['Sender']; ?></td>
                                    <td><?php echo $row['Receiver']; ?></td>
                                    <td><?php echo $row['SendDate']; ?></td>
                                    <td><?php echo $dispatched; ?></td>
                                    <td>Placeholder</td>
                                    <td><a href="yourfilename.php?id=<?php echo $row['ID']; ?>">Delete</a>
                                        <button data-target="modal1" class="modal-trigger">Modal</button>
                                    </td>
                                </tr>
                                <?php

                            }
                            ?>
                        </table>
                    </div>
                </div>
            </form>

相关问题