0

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); ?>
4
  • What do you expect the value of $row['ReqID'] to be where you have your delete query? Commented Feb 24, 2012 at 2:47
  • The value of the records unique ID thats comes from the sql database Commented Feb 24, 2012 at 2:49
  • Hmmm okay. Well I don't understand why you are surprised that it's deleting the first row. Your first code needs to passing along the request of the id you want to delete to the second page. It's not doing that. Commented Feb 24, 2012 at 2:53
  • Ahh right, how would I go about doing that then? Sorry if I sound dumb Commented Feb 24, 2012 at 2:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.