I have a PHP page that shows me data from the database. I’m inserting a button that, if pressed, deletes the record. The problem is that if I push the button the action is done on all the records, because I can’t uniquely identify the click on the button
some code
foreach ( $associati as $associato ) {
echo "<hr />";
echo "Nome associato : ".$associato->Nome."</br>";
echo "Codice Fiscale : ".$associato->CF."</br>";
echo "<hr />";
if(isset($_POST["numerazione"])){
echo "Hello world"; //Query for delete
}
?>
<form method="POST" action="">
<input type="submit"
name="numerazione"
value="Elimina utente"
onclick="return confirm('Are you sure?')"
/>
</form>
<?php
}
How can I do to uniquely identify the button?
3
Answers
Solution for this problem:
Pass the unique information (e.g.
$id
or$associato->id
or whatever the variable which can identify the record) when the form is submittedYou can add a hidden field to each form that contains the unique identifier of the data, that means when you click the button, it will create a POST request, and in that POST request you can get the ID of the clicked record by doing $_POST[‘unique-id’], also make sure to populate the value of that hidden field using PHP