Index: nodecomment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nodecomment/nodecomment.module,v
retrieving revision 1.3.2.15.2.17.2.6
diff -u -r1.3.2.15.2.17.2.6 nodecomment.module
--- nodecomment.module	27 May 2009 19:16:06 -0000	1.3.2.15.2.17.2.6
+++ nodecomment.module	27 May 2009 21:22:21 -0000
@@ -176,7 +176,7 @@
         }
 
         // Load the target node. This is the node type that we fetch the settings for.
-        $target = node_load(isset($node->comment_target_cid) ? $node->comment_target_cid : $node->comment_target_nid);
+        $target = node_load(!empty($node->comment_target_cid) ? $node->comment_target_cid : $node->comment_target_nid);
 
         $anon_meta_info = variable_get('comment_anonymous_'. $target->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT);
         if ($user->uid == 0 && ($anon_meta_info == COMMENT_ANONYMOUS_MAY_CONTACT || $anon_meta_info == COMMENT_ANONYMOUS_MUST_CONTACT)) {
@@ -238,8 +238,8 @@
         $form['#node']->comment_target_nid = arg(3);
 
         // If the reply link belonged to a comment node, get that node's id as well.
-        if (is_numeric(arg(4))) {
-          $form['#node']->comment_target_cid = arg(4);
+        if (!isset($form['#node']->nid)) {
+          $form['#node']->comment_target_cid = is_numeric(arg(4)) ? arg(4) : 0;
         }
 
         // Always show the node to which this comment is replying
@@ -336,7 +336,9 @@
       if ($comment_data['cid']) {
         // It's a node comment! Populate commenty stuff.
         $comment_data['comment_target_nid'] = $comment_data['nid'];
+        $comment_data['comment_target_cid'] = $comment_data['pid'];
         unset($comment_data['nid']);
+        unset($comment_data['pid']);
         return $comment_data;
       }
       else if ($node->comment_type) {
@@ -349,6 +351,7 @@
       break;
 
     case 'insert':
+    case 'update':
       // if this is a comment, save it as a comment
       if (isset($node->comment_target_nid)) {
         nodecomment_save($node);
@@ -395,10 +398,12 @@
 
   $node->thread = nodecomment_get_thread($node);
 
-  db_query("INSERT INTO {node_comments} ".
-         "(cid,        nid,                       pid,                       hostname,                thread,        name,        uid,        mail,        homepage)".
-  "VALUES (%d,         %d,                        %d,                        '%s',                    '%s',          '%s',        %d,         '%s',        '%s')",
-           $node->nid, $node->comment_target_nid, $node->comment_target_cid, ip_address(), $node->thread, $node->name, $node->uid, $node->mail, $node->homepage);
+  // Try an update first, do not change the original submitted IP Address.
+  db_query("UPDATE {node_comments} SET nid = %d, pid = %d, thread = %d, name = '%s', uid = %d, mail = '%s', homepage = '%s' WHERE cid = %d", $node->comment_target_nid, $node->comment_target_cid, $node->thread, $node->name, $node->uid, $node->mail, $node->homepage, $node->nid);
+  // If not updated, insert a new comment.
+  if (db_affected_rows() == 0) {
+    db_query("INSERT INTO {node_comments} (cid, nid, pid, hostname, thread, name, uid, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', '%s', %d, '%s', '%s')", $node->nid, $node->comment_target_nid, $node->comment_target_cid, ip_address(), $node->thread, $node->name, $node->uid, $node->mail, $node->homepage);
+  }
 
   _nodecomment_update_node_statistics($node->comment_target_nid);
 
