Say, I am editing a profile of a user, with a HTML of something like this:
<form action="" method="POST">
<?php
$count = 0;
foreach($fetchResult as $data){
?>
<input type="text" name="data[<?php echo $count ?>][name]" value="<?php echo $data['Name'] ?>">
<input type="hidden" name="data[<?php echo $count ?>][id]" value="<?php echo $data['DataId']; ?>">
<?php
$count++;
}
?>
</form>
And I can add as many of this (and delete) dynamically (adding of elements not included), but without the hidden
input, because the hidden
input will only appear during foreach
of the data, and also new input does not really any id yet.
I managed to do the edit and insert part, with something like this:
foreach ($data as $datum) {
if (empty($datum['id'])) {
//insert the new inputs
} else {
//update the input
}
}
I solved this before by just deleting the data and inserting again, but this can't be used if the data are connected to others in a database (as a foreign key).
What other ways can be done to check if what id
s are removed after submitting?