diff --git a/include/class.attachment.php b/include/class.attachment.php
index c605b7ca03d6ed8e0d5a4818d110b4ba16df7307..09d9826fd0444748830e63b18e5caf9bb5441f54 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -128,7 +128,10 @@ class GenericAttachments {
                     .',object_id='.db_input($this->getId())
                     .',file_id='.db_input($fileId)
                     .',inline='.db_input($inline ? 1 : 0);
-                if (db_query($sql))
+                // File may already be associated with the draft (in the
+                // event it was deleted and re-added)
+                if (db_query($sql, function($errno) { return $errno != 1062; })
+                        || db_errno() == 1062)
                     $i[] = $fileId;
             }
         }
diff --git a/include/mysqli.php b/include/mysqli.php
index 86b5a3b7fe0c6a789dfa515afae6f52ce793a76d..4a477bea5b3b5e9d696aaadd25cb30bec65a45d3 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -121,7 +121,22 @@ function db_create_database($database, $charset='utf8',
             $database, $charset, $collate));
 }
 
-// execute sql query
+/**
+ * Function: db_query
+ * Execute sql query
+ *
+ * Parameters:
+ * $query - (string) SQL query (with parameters)
+ * $logError - (mixed):
+ *      - (bool) true or false if error should be logged and alert email sent
+ *      - (callable) to receive error number and return true or false if
+ *      error should be logged and alert email sent. The callable is only
+ *      invoked if the query fails.
+ *
+ * Returns:
+ * (mixed) MysqliResource if SELECT query succeeds, true if an INSERT,
+ * UPDATE, or DELETE succeeds, false or null if the query fails.
+ */
 function db_query($query, $logError=true) {
     global $ost, $__db;
 
@@ -134,6 +149,10 @@ function db_query($query, $logError=true) {
     } while (!$res && --$tries && $__db->errno == 1213);
 
     if(!$res && $logError && $ost) { //error reporting
+        // Allow $logError() callback to determine if logging is necessary
+        if (is_callable($logError) && !($logError($__db->errno)))
+            return $res;
+
         $msg='['.$query.']'."\n\n".db_error();
         $ost->logDBError('DB Error #'.db_errno(), $msg);
         //echo $msg; #uncomment during debuging or dev.