diff --git a/domain_theme/domain_theme.module b/domain_theme/domain_theme.module
index a165ecf..247f0ca 100644
--- a/domain_theme/domain_theme.module
+++ b/domain_theme/domain_theme.module
@@ -222,7 +222,7 @@ function domain_theme_domainbatch() {
     ),
     '#domain_action' => 'custom',
     '#lookup' => 'domain_theme_batch_lookup',
-    '#submit' => 'domain_theme_batch',
+    '#submit' => 'domain_theme_batch_submit',
     '#system_default' => variable_get('theme_default', 'garland'),
     '#variable' => 'theme_default',
     '#meta_description' => t('Change the themes for all domains.'),
@@ -243,17 +243,30 @@ function domain_theme_domainbatch() {
  * @param $values
  * The form values passed by the FormsAPI.
  */
-function domain_theme_batch($values) {
+function domain_theme_batch_submit($values) {
   foreach ($values['domain_batch'] as $key => $value) {
     if ($key > 0) {
       // Clear out the old theme.
-      db_query("UPDATE {domain_theme} SET status = 0 WHERE domain_id = %d", $key);
-      $data = db_fetch_array(db_query("SELECT theme FROM {domain_theme} WHERE theme = '%s' AND domain_id = %d", $value, $key));
-      if (isset($data['theme'])) {
-        db_query("UPDATE {domain_theme} SET status = 1 WHERE theme = '%s' AND domain_id = %d", $value, $key);
+      db_update('domain_theme')
+        ->fields(array('status' => 0))
+        ->condition('domain_id', $key)
+        ->execute();
+      $data = db_query("SELECT theme FROM {domain_theme} WHERE theme = :theme AND domain_id = :domain_id",
+        array(':theme' => $value, ':domain_id' => $key))->fetchField();
+      if (!empty($data) && $data == $value) {
+        db_update('domain_theme')
+          ->fields(array('status' => 1))
+          ->condition('domain_id', $key)
+          ->condition('theme', $value)
+          ->execute();
       }
       else {
-        db_query("INSERT INTO {domain_theme} (domain_id, theme, status) VALUES (%d, '%s', 1)", $key, $value);
+        db_insert('domain_theme')
+          ->fields(array(
+            'domain_id' => $key,
+            'theme' => $value,
+            'status' => 1,
+        ))->execute();
       }
     }
     else {
