从 PHP while 循环中删除 MySQL 行 [英] Delete MySQL row from PHP while loop

查看:37
本文介绍了从 PHP while 循环中删除 MySQL 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 while 循环在表中显示我的数据库列和行.

我想制作一个可以删除特定 MySQL 行的按钮,但不幸的是,我无法选择必须删除所有行中的哪一行,因为我使用while 循环"以 HTML 格式显示我的数据库内容.

提供图片以更好地理解我想做什么:
是的,我希望在点击绿色按钮时发送 MySQL 查询以删除该行(存在于我的数据库中).

提供代码:

query($sql);如果 ($result->num_rows > 0){//表格标题 - 不是 PHPecho "<tr><th style='background-color: #2d323a;颜色:白色;'>日期</th><th style='background-color: #2d323a;颜色:白色;'>数量<th style='background-color: #2d323a;颜色:白色;'>原因</th></tr>";//输出每一行的数据while($row = $result->fetch_assoc()){//RED//如果有"- ",则将金额声明为红色if(strpos($row['amount'],'-') !== false){回声"<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//如果没有"- ",则使金额声明正常别的{回声"<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>";}别的{echo "0 结果";}

解决方案

首先,表头只使用

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

在这种情况下,选择删除"链接时,它将使用$ _get变量调用相同的脚本:您可以测试的dellesid",您可以测试,如果找到,则执行删除行.您也可以将表格嵌入到html表单中,并在每行的单元格中添加一个提交按钮,提交的值设置为要删除的行id.

I have a while loop displaying my database columns and rows in table.

I want make a button which can delete specific MySQL row but unfortunately I can't select which of all rows have to be deleted simply because I use "while loop" to display my database content in HTML.

Providing picture for better understanding what I want to do:
So yeah, I want that when the green button is clicked - send MySQL query to delete this row (which is existing in my DB).

Providing code:

<?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";
        }

解决方案

First, only use <th> elements for the table headers. For the actual data cells use the <td> element.

To allow deletion of individual database rows you can include links in your table. In your loop that creates the table rows:

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

In this case, when the "Delete" link is selected it calls your same script with a $_GET variable: "deleteId" which you can test for and if found, perform the delete of the row. You can also embed the table in an html form and add a submit button to a cell in each row, with the value of the submit set to the row id to delete.

这篇关于从 PHP while 循环中删除 MySQL 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆
元素.对于实际的数据单元格,请使用 元素.

要允许删除单个数据库行,您可以在表中包含链接.在创建表行的循环中: