diff --git a/mod/glossary/edit_form.php b/mod/glossary/edit_form.php
index d45e7e2..414bc47 100644
--- a/mod/glossary/edit_form.php
+++ b/mod/glossary/edit_form.php
@@ -107,21 +107,21 @@ class mod_glossary_entry_form extends moodleform {
                 }
             }
             if (!$glossary->allowduplicatedentries) {
-                if ($dupentries = $DB->get_records('glossary_entries', array('LOWER(concept)'=>moodle_strtolower($data['concept'])))) {
-                    foreach ($dupentries as $curentry) {
-                        if ($glossary->id == $curentry->glossaryid) {
-                           if ($curentry->id != $id) {
-                               $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
-                               break;
-                           }
-                        }
-                    }
+                if ($DB->record_exists_select('glossary_entries',
+                        'glossaryid = :glossaryid AND LOWER(concept) = :concept AND id != :id', array(
+                            'glossaryid' => $glossary->id,
+                            'concept'    => moodle_strtolower($data['concept']),
+                            'id'         => $id))) {
+                    $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
                 }
             }
 
         } else {
             if (!$glossary->allowduplicatedentries) {
-                if ($dupentries = $DB->get_record('glossary_entries', array('LOWER(concept)'=>moodle_strtolower($data['concept']), 'glossaryid'=>$glossary->id))) {
+                if ($DB->record_exists_select('glossary_entries',
+                        'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
+                            'glossaryid' => $glossary->id,
+                            'concept'    => moodle_strtolower($data['concept'])))) {
                     $errors['concept'] = get_string('errconceptalreadyexists', 'glossary');
                 }
             }
diff --git a/mod/glossary/import.php b/mod/glossary/import.php
index 3251306..9d6b7c1 100644
--- a/mod/glossary/import.php
+++ b/mod/glossary/import.php
@@ -206,7 +206,7 @@ if ($xml = glossary_read_imported_file($result)) {
                 if ( $newentry->casesensitive ) {
                     $dupentry = $DB->get_record("glossary_entries", array("concept"=>$newentry->concept, "glossaryid"=>$glossary->id));
                 } else {
-                    $dupentry = $DB->get_record("glossary_entries", array("lower(concept)"=>moodle_strtolower($newentry->concept), "glossaryid"=>$glossary->id));
+                    $dupentry = $DB->get_record_sql("SELECT LOWER(concept) FROM {glossary_entries} WHERE LOWER(concept) = ?", array(moodle_strtolower($newentry->concept)));
                 }
                 if ($dupentry) {
                     $permissiongranted = 0;
