The update function is similar, passing the module’s ID to the insert_record() function for each image, The difference here is that we delete the existing image records in advance. The end result is as shown.
function draganddrop_update_instance($draganddrop) {
$draganddrop->timemodified = time();
$draganddrop->id = $draganddrop->instance;
$image1->ddid = $draganddrop->id;
$image1->url = $draganddrop->image1;
$image2->ddid = $draganddrop->id;
$image2->url = $draganddrop->image2;
$image3->ddid = $draganddrop->id;
$image3->url = $draganddrop->image3;
//delete all images
delete_records(“draganddrop_images”, “ddid”, “$draganddrop->id”);
insert_record(“draganddrop_images”, $image1);
insert_record(“draganddrop_images”, $image2);
insert_record(“draganddrop_images”, $image3);
return update_record(“draganddrop”, $draganddrop);
}
The delete function removes all the images prior to deleting the master record. This is done to ensure that no foreign key restraints prevent the deletion of child records from the images table. The delete_records() function automatically generates and executes the SQL statements based on the parameters passed.

function draganddrop_delete_instance($id) {
if (! $draganddrop = get_record(“draganddrop”, “id”, “$id”)) {
return false;
}
$result = true;
if (! delete_records(“draganddrop_images”, “ddid”, “$draganddrop->id”)) {
$result = false;
}
if (! delete_records(“draganddrop”, “id”, “$draganddrop->id”)) {
$result = false;
}
return $result;
}