--- editrandom_orig.php 2020-03-09 19:29:59.000000000 +0100 +++ editrandom.php 2020-05-15 03:56:03.229863800 +0200 @@ -133,10 +133,8 @@ }, $currentslottags); foreach ($tags as $tag) { - if ($key = array_search(['tagid' => $tag->id, 'tagname' => $tag->name], $searchableslottags)) { - // If found, $key would be the id field in the quiz_slot_tags table. - // Therefore, there was no need to check !== false here. - $recordstokeep[] = $key; + if (in_array(['tagid' => $tag->id, 'tagname' => $tag->name], $searchableslottags)) { + $recordstokeep[] = $tag->id; } else { $recordstoinsert[] = (object)[ 'slotid' => $slot->id, @@ -149,7 +147,7 @@ // Now, delete the remaining records. if (!empty($recordstokeep)) { list($select, $params) = $DB->get_in_or_equal($recordstokeep, SQL_PARAMS_QM, 'param', false); - $DB->delete_records_select('quiz_slot_tags', "id $select", $params); + $DB->delete_records_select('quiz_slot_tags', "slotid = {$slot->id} AND tagid {$select}", $params); } else { $DB->delete_records('quiz_slot_tags', array('slotid' => $slot->id)); }