Unable to pass hidden form value from database to PHP if-statement

My database table (related) contains 3 columns:

  • related_id
  • article_id
  • object_id

This is a table that tracks the relationship between articles and objects. I stripped the code. Now it just contains a delete button (x). If someone clicked this button, I want the user to be redirected if(isset($_POST['deleteRelated']))to receive the message "Are you sure", etc. But the hidden identifier is not passed correctly. Last related_idin table 29 . When I try to revoke a hidden id, I just get 29 for each delete button (x).

The full version of the code below gives me a table with the title of the article, the name of the object, and the delete button (x). Since the submit button cannot pass the value, I need a hidden value. But when I transfer it by pressing the delete button (x), I get 29 every time.

the 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 print:

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

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

Can someone solve this problem? Shouldn't it be that hard?

+4
source share
5 answers

Description

<form> , () <input type="hidden" name="hidden-id" value="..."> s.

( , , hidden-id, submit, ?). last hidden-id - , /.

<form> <form> :

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>';

}

hidden-id .

+4

, .

:

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

, :

$related_id = $_POST['deleteRelated'];
+2

foreach. submit. :

foreach ($result as $related) {

echo '
<form method="POST" action="...">
<input type="hidden" name="hidden-id" value="' . $related['related_id'] . '">
<input type="submit" name="deleteRelated" value="x">
</form>';}
+2

, ? :

<input type="hidden" name="hidden-id" value="" />

- :

echo '<input type="submit" name="deleteRelated" value="x" onclick="setValue('" . $related['related_id'] . "'); />';

js:

setValue(val) {
    document.getElementsByName("hidden-id")[0].value = val;
}
+2

: form foreach

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

foreach ($result as $related) 
{
    echo '<form method="post">';

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

    echo '</form>';
}
+1

All Articles