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?