简体   繁体   English

PHP-MySQL删除记录

[英]PHP - MySQL Deleting a record

I'm pulling records from the database and created a PHP file to execute when then "delete" button is pressed. 我正在从数据库中提取记录,并创建了一个PHP文件以在按下“删除”按钮时执行。 The code works in the sense that it deletes the records from the database but it always deletes the first record in the database and not the one relating to the Request ID thats in the table. 该代码在某种意义上起作用,即从数据库中删除记录,但始终删除数据库中的第一条记录,而不删除与表中的请求ID有关的一条记录。 Any help would be great 任何帮助都会很棒

Table: 表:

<?php

    //3. Perform database query
        $result = mysql_query("SELECT * FROM Request
        WHERE DepID = 'CO'
        AND Status = 'P'", $connection);
        if(!$result){
            die("Database query failed: " . mysql_error());
        }

    //4. Use Returned Data
    while ($row = mysql_fetch_array($result)) {
    $row['ReqID'];  

        echo "<tr>";
        echo "<td class='req' align='center'>".$row['ReqID']."</td>";
        echo "<td class='req' align='center'>".$row['ModTitle']."</td>"; 
        echo "<td class='req' align='center'>".$row['BuildingID']."</td>";
        echo "<td class='req' align='center'>".$row['RoomID']."</td>";
        echo "<td class='req' align='center'>".$row['Priority']."</td>";
        echo "<td class='req' align='center'>".$row['W1']."</td>";
        echo "<td class='req' align='center'>".$row['P1']."</td>";
        echo "<td class='req' align='center'><a href=''><img height='18px' src='images/edit.png'></a></td>";
        echo "<td class='req' align='center'><a href='deletereq.php'><img src='images/exit.png'></a></td>";
        echo "</tr>";




    }
?>

And then this is the deletereq.php file: 然后这是deletereq.php文件:

<?php require_once("includes/connection.php"); ?>

<?php

    //3. Perform database query
        $result = mysql_query("SELECT * FROM Request
        WHERE DepID = 'CO'
        AND Status = 'P'", $connection);
        if(!$result){
            die("Database query failed: " . mysql_error());
        }

    //4. Use Returned Data
    while ($row = mysql_fetch_array($result)) {
    $row['ReqID'];


$result2 = mysql_query("DELETE FROM Request 
                       WHERE ReqID = {$row['ReqID']} LIMIT 1", $connection);
    if (mysql_affected_rows() == 1) {
                redirect_to("content.php");
            } else {
            //deletion failed
            echo "<h1>Request Deletion Failed.</h1>";
            echo "<p>" . mysql_error() . "</p>";
            echo "<a href=\"request.php\"> Return to request</a>";
        }
        }
?>

<?php mysql_close($connection); ?>

On display page 在显示页面上

echo "<td class='req' align='center'><a href='deletereq.php?id=".$row['ReqID']."'><img src='images/exit.png'></a></td>";

And on your delete page change the query to use the value of $_GET['id'] 在您的删除页面上,更改查询以使用$ _GET ['id']的值

But please note that depending on your use and such here you may really want to figure out what your needs are from a security etc perspective. 但是请注意,根据您的用途以及此处的情况,您可能真的想从安全性等角度确定您的需求。 Additionally generally speaking using a GET for change is not a good idea, it would be better to use a POST and then a redirect. 另外,一般来讲,使用GET进行更改不是一个好主意,最好先使用POST,然后再使用重定向。 Point being the above will work but that (and what you already have) is enough that you could do some serious damage to yourself if not careful. 要点就是上面的方法可以工作,但是那(以及您已经拥有的)就足够了,如果不小心的话,可能会对自己造成一些严重的损害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM