簡體   English   中英

從PHP while循環中刪除MySQL行

[英]Delete MySQL row from PHP while loop

我有一個while循環,顯示表中的數據庫列和行。

我想創建一個可以刪除特定MySQL行的按鈕,但是不幸的是,我無法選擇所有行中的哪一個必須刪除,僅因為我使用“ while循環”以HTML格式顯示數據庫內容。

提供圖片以更好地理解我想要做什么: 在此處輸入圖片說明
是的,我想要單擊綠色按鈕時-發送MySQL查詢以刪除此行(該行已存在於我的數據庫中)。

提供代碼:

<?php
//CONECT TO MYSQL
include 'database.php';

//GET VALUES
        $sql = "SELECT * FROM amountcontainer";
        $result = $conn->query($sql);


        if ($result->num_rows > 0) 
        {
            //Table headlines - NOT A PHP
            echo "<table class='moneytable'>
                    <tr>
                        <th style='background-color: #2d323a; color: white;'>Date</th>
                        <th style='background-color: #2d323a; color: white;'>Amount</th>
                        <th style='background-color: #2d323a; color: white;'>Reason</th>
                    </tr>";

            // output data of each row
            while($row = $result->fetch_assoc()) 
            {

                //RED //Make statement of amount red if it has a " - "
                if(strpos($row['amount'],'-') !== false)
                {

                    echo 
                    "
                        <tr>
                            <th style='font-weight: normal;background-color: #ff9999;' class='row-time'>" . $row['time'] . "</th>
                            <th style='font-weight: normal;background-color: #ff9999;' class='row-amount'>" . $row['amount'] . "</th>
                            <th style='font-weight: normal;background-color: #ff9999;' class='row-reason'>" . $row['reason'] . "</th>
                        </tr>

                    ";

                }

                //NORMAL //Make statement of amount normal if it doesn't have a " - "
                else
                {
                    echo 
                    "
                        <tr>
                            <th style='font-weight: normal;' class='row-time'>" . $row['time'] . "</th>
                            <th style='font-weight: normal;' class='row-amount'>" . $row['amount'] . "</th>
                            <th style='font-weight: normal;' class='row-reason'>" . $row['reason'] . "</th>
                        </tr>

                    ";

                }


            }
            echo "</table>";
        } 

        else 
        {
            echo "0 results";
        }

這聽起來像是ajax的地方。 這是一個簡單的示例,可能會為您提供一個良好的起點。

<?php
while($row = $result->fetch_assoc()) {
    $isNegative = strpos($row['amount'],'-') !== false;

    echo "<tr class='record' data-id=\"{$row["id"]}\">";
    //Use ternary operator & css classes
    echo "<td class='standard-record ".$isNegative ? "color-red" : "" ."''>{$row["record"]}</td>";
    echo "</tr>";
}
?>
<script>
    $(".record").click(function(){
        var request = {
            id: $(this).data("id")
        };
        $.post( "delete-record.php", request, function(data){
            $(this).remove();
        });
    });
</script>

改變這個

if(strpos($row['amount'],'-') !== false) # wrong way

對此

if($row['amount'] < 0) # detecting negative numbers

首先,僅將<th>元素用於表頭。 對於實際的數據單元,請使用<td>元素。

要允許刪除單個數據庫行,可以在表中包括鏈接。 在創建表行的循環中:

                    <tr>
                        <td>" . $row['time'] . "</td>
                        <td>" . $row['amount'] . "</td>
                        <td>" . $row['reason'] . "</td>
                        <td><a href='?deleteId=$row[keyID]'>Delete</a></td> 
                    </tr>

在這種情況下,當選擇“刪除”鏈接時,它將使用$ _GET變量調用同一腳本:“ deleteId”,您可以測試該變量,如果找到,則執行該行的刪除。 您還可以將表格嵌入html表單中,並將提交按鈕添加到每一行的單元格中,並將提交的值設置為要刪除的行ID。

您需要添加一個額外的

<th style='font-weight: normal;' class='row-reason'><a class="crossBtn" href="?del_id=".$row['id'].">&nbsp;</a></th>

並獲取del_id並刪除此特定記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM