繁体   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