Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't pass hidden form value from database to PHP if-statement

My database table (Related) contains 3 columns:

  • related_id
  • article_id
  • object_id

It's a table which keeps track of relationships between articles and objects. I have stripped the code. Now it just contains the delete button (x). If someone press that button I want the user to be redirected to if(isset($_POST['deleteRelated'])) to get a "Are you sure"-message etc. But the hidden ID isn't passed correctly. The last related_id in the table is 29. When I try to echo out the hidden ID i just get 29 for every delete button (x).

The complete version of the code below gives me a table with the article title, object title and the delete button (x). Because of a submit button can't pass a value by itself I need a hidden value. But when I pass it by pressing the delete button (x) i just gets 29 every time.

Code

if(isset($_POST['deleteRelated'])) {

    echo $_POST['hidden-id']; // Will just display the last 'related_id' value.

else {

    echo '
    <form method="post">';

    $stmt = $db->prepare("SELECT * FROM Related");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

    foreach ($result as $related) {

        echo '
        <input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
        <input type="submit" name="deleteRelated" value="x">';

    }

    echo '
    </form>';

}

If I type:

<input type="submit" name="deleteRelated" value="' . $related['related_id'] . '">

It will display the correct value from the database instead of the x for the delete/submit button. But when I press the delete/submit button I just get the last related_id which is 29.

Can someone solve this problem? Should not be that hard?

like image 577
Treps Avatar asked Dec 19 '22 10:12

Treps


2 Answers

Explanation

You have one <form> for the entire table, which includes (say) a dozen <input type="hidden" name="hidden-id" value="...">s.

Those values will ALL be sent to the server when the form is submitted (how do you expect it to know to only send the hidden-id which is next to the specific submit button that was pressed?) This is why you are only seeing the last hidden-id – they are all being sent, so the final one overrides/wins.

Solution

One solution would be to have a <form> per row instead of one <form> for the whole table:

foreach ($result as $related) {

    echo '<form method="POST" action="...">';

    echo '
    <input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
    <input type="submit" name="deleteRelated" value="x">';

    echo '</form>';

}

That way, only the hidden-id value of the pressed button would be sent.

like image 179
Shai Avatar answered Jan 13 '23 12:01

Shai


Alternatively, you really don't need a form for each row, you could use a button here instead and ditching those hidden inputs.

Example:

foreach ($result as $related) {
    echo '<button type="submit" name="deleteRelated" value="' . $related['related_id'] . '">Delete</button>';
}

So now each value of that pressed button on each row will go to:

$related_id = $_POST['deleteRelated'];
like image 37
Kevin Avatar answered Jan 13 '23 11:01

Kevin