简体   繁体   中英

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. 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. 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:

<?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']

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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