--- contrib/image_attach/image_attach.module.orig	2010-04-13 14:44:21.000000000 -0400
+++ contrib/image_attach/image_attach.module	2010-04-13 13:39:42.000000000 -0400
@@ -208,8 +208,6 @@ function image_attach_form_alter(&$form,
       $form['#attributes'] = array("enctype" => "multipart/form-data");
       // Add a custom submit handler so we can handle image creation on-the-fly
       $form['#validate'][] = 'image_attach_validate';
-      // Add a custom submit handler so we can clean up the selection box items.
-      $form['#submit'][] = 'image_attach_node_form_submit';
 
       // Check permissions and settings
       $may_attach           = user_access('attach images');
@@ -314,9 +312,9 @@ function image_attach_image_add_submit(&
   if (isset($form_state['values']['iids'])) {
     db_query("DELETE FROM {image_attach} WHERE nid = %d", $form['nid']['#value']);
     if (count($form_state['values']['iids'])) {
-      $weight = 0;
+      $nid = $form['nid']['#value'];
       foreach ($form_state['values']['iids'] as $iid) {
-        db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $form['nid']['#value'], $iid, $weight++);
+        _image_attach_write_record($nid, $iid);
       }
     }
   }
@@ -394,15 +392,6 @@ function image_attach_validate(&$form, &
 }
 
 /**
- * Extra submit handler for node forms.
- */
-function image_attach_node_form_submit(&$form, &$form_state) {
-  // Clear the 0 key in the iids array that arises from selecting the 'None'
-  // option. We do this here so image_attach_nodeapi() gets clean data.
-  unset($form_state['values']['iids'][0]);
-}
-
-/**
  * Implementation of hook_nodeapi().
  */
 function image_attach_nodeapi(&$node, $op, $teaser, $page) {
@@ -423,9 +412,8 @@ function image_attach_nodeapi(&$node, $o
       db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
       if (!empty($node->iids)) {
         // Populate weight column with placeholder values.
-        $weight = 0;
         foreach ($node->iids as $iid) {
-          db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++);
+          _image_attach_write_record($node->nid, $iid);
         }
       }
       break;
@@ -513,6 +501,63 @@ function _image_attach_node_load_attache
 }
 
 /**
+ * Function to insert values into image_attach db table
+ */
+function _image_attach_write_record($nid, $iid) {
+  static $table = 'image_attach';
+  static $weights;
+
+  // validation
+  if (!$nid || !$iid) {
+    return FALSE;
+  }
+
+  // initialize weights
+  if (!isset($weights[$nid])) {
+    $weights[$nid] = 0;
+  }
+
+  // build record
+  $record = new stdClass();
+  $record->nid = $nid;
+  $record->iid = $iid;
+  $record->weight = $weights[$nid];
+
+
+  // determine if this is an update
+  $pkeys = array();
+  if (_image_attach_record_exists($nid, $iid)) {
+    $pkeys = array('nid', 'iid');
+  }
+
+  // write record
+  drupal_write_record($table, $record, $pkeys);
+
+  // increment node's weight
+  $weights[$nid]++;
+
+  return $record;
+}
+
+/**
+ * Function to determine if record exists in image_attach db table
+ */
+function _image_attach_record_exists($nid, $iid) {
+  static $table = 'image_attach';
+  static $cache;
+
+  // check cache
+  if (isset($cache[$nid][$iid])) {
+    return $cache[$nid][$iid];
+  }
+
+  // run query
+  $count = db_result(db_query("SELECT COUNT(nid) FROM {" . $table . "} WHERE nid=%d AND iid=%d", $nid, $iid));
+  $cache[$nid][$iid] = $count;
+  return $count;
+}
+
+/**
  * Fetch an array of all candidate referenced nodes, for use in presenting the
  * selection form to the user.
  *
