Index: support/support.module
===================================================================
--- support/support.module	(revision 121)
+++ support/support.module	(working copy)
@@ -657,8 +657,17 @@
             $node->assigned = 0;
           }
         }
-        db_query("UPDATE {support_ticket} SET message_id = '%s', state = %d, priority = %d, client = %d, assigned = %d WHERE nid = %d", isset($node->message_id) ? $node->message_id : '', $node->state, $node->priority, $node->client, $node->assigned, $node->nid);
-        if (!db_affected_rows()) {
+        $ticket_exists = db_result(db_query(
+            "SELECT COUNT(*)
+              FROM {support_ticket}
+              WHERE nid = %d",
+            $node->nid
+          )
+        );
+        if ($ticket_exists) {
+          db_query("UPDATE {support_ticket} SET message_id = '%s', state = %d, priority = %d, client = %d, assigned = %d WHERE nid = %d", isset($node->message_id) ? $node->message_id : '', $node->state, $node->priority, $node->client, $node->assigned, $node->nid);
+        }
+        else {
           db_query("INSERT INTO {support_ticket} (nid, message_id, state, priority, client, assigned) VALUES(%d, '%s', %d, %d, %d, %d)", $node->nid, isset($node->message_id) ? $node->message_id : '', $node->state, $node->priority, $node->client, $node->assigned);
         }
         if ($op == 'insert') {
@@ -750,8 +759,17 @@
             $comment['assigned'] = 0;
           }
         }
-        db_query("UPDATE {support_ticket_comment} SET message_id = '%s', state = %d, priority = %d, client = %d, assigned = %d WHERE cid = %d", isset($comment['message_id']) ? $comment['message_id'] : '', $comment['state'], $comment['priority'], $comment['client'], $comment['assigned'], $comment['cid']);
-        if (!db_affected_rows()) {
+        $comment_exists = db_result(db_query(
+            "SELECT COUNT(*)
+              FROM {support_ticket_comment}
+              WHERE cid = %d",
+            $comment['cid']
+          )
+        );
+        if ($comment_exists) {
+          db_query("UPDATE {support_ticket_comment} SET message_id = '%s', state = %d, priority = %d, client = %d, assigned = %d WHERE cid = %d", isset($comment['message_id']) ? $comment['message_id'] : '', $comment['state'], $comment['priority'], $comment['client'], $comment['assigned'], $comment['cid']);
+        }
+        else {
           db_query("INSERT INTO {support_ticket_comment} (cid, message_id, state, priority, client, assigned) VALUES(%d, '%s', %d, %d, %d, %d)", $comment['cid'], isset($comment['message_id']) ? $comment['message_id'] : '', $comment['state'], $comment['priority'], $comment['client'], $comment['assigned']);
         }
         _support_comment_update_node($comment['nid']);
@@ -1353,8 +1371,18 @@
   $client = support_client_load($clid);
   $account = user_load(array('uid' => $uid));
   if (support_access_clients($client, $account)) {
-    db_query('UPDATE {support_assigned} SET active = %d WHERE uid = %d AND nid = %d', $active, $uid, $nid);
-    if (!db_affected_rows()) {
+    $assignment_exists = db_result(db_query(
+        "SELECT COUNT(*)
+          FROM {support_assigned}
+          WHERE uid = %d AND nid = %d",
+        $uid,
+        $nid
+      )
+    );
+    if ($assignment_exists) {
+      db_query('UPDATE {support_assigned} SET active = %d WHERE uid = %d AND nid = %d', $active, $uid, $nid);
+    }
+    else {
       db_query('INSERT INTO {support_assigned} (nid, uid, active) VALUES(%d, %d, %d)', $nid, $uid, $active);
     }
   }
Index: support/support.admin.inc
===================================================================
--- support/support.admin.inc	(revision 120)
+++ support/support.admin.inc	(working copy)
@@ -254,13 +254,13 @@
     db_query('DELETE FROM {support_client} WHERE clid = %d', $form_state['values']['clid']);
   }
   else {
-    db_query("UPDATE {support_client} SET name = '%s', path = '%s', status = %d, integrate_email = %d, server_name = '%s', server_username = '%s', server_password = '%s', mailbox = '%s', mailfrom = '%s', protocol = %d, extra = '%s', port = %d, autoassign = '%s', autosubscribe = '%s', thread_subject = %s, domains = '%s', user_creation = '%s', notes = '%s' WHERE clid = %d", check_plain($form_state['values']['name']), $form_state['values']['path'], $form_state['values']['status'], $form_state['values']['integrate_email'], $form_state['values']['server_name'], $form_state['values']['server_username'], $form_state['values']['server_password'], $form_state['values']['mailbox'], $form_state['values']['mailfrom'], $form_state['values']['protocol'], $form_state['values']['extra'], $form_state['values']['port'], $form_state['values']['autoassign'], $form_state['values']['autosubscribe'], $form_state['values']['thread_subject'], $form_state['values']['domains'], $form_state['values']['user_creation'], $form_state['values']['notes'], $form_state['values']['clid']);
-    if (!db_affected_rows()) {
-      drupal_set_message(t('Client %client added.', array('%client' => $form_state['values']['name'])));
-      db_query("INSERT INTO {support_client} (name, path, status, integrate_email, server_name, server_username, server_password, mailbox, mailfrom, protocol, extra, port, autoassign, autosubscribe, thread_subject, domains, user_creation, notes) VALUES('%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%d', '%s', '%s', '%s')", check_plain($form_state['values']['name']), $form_state['values']['path'], $form_state['values']['status'], $form_state['values']['integrate_email'], $form_state['values']['server_name'], $form_state['values']['server_username'], $form_state['values']['server_password'], $form_state['values']['mailbox'], $form_state['values']['mailfrom'], $form_state['values']['protocol'], $form_state['values']['extra'], $form_state['values']['port'], $form_state['values']['autoassign'], $form_state['values']['autosubscribe'], $form_state['values']['thread_subject'], $form_state['values']['domains'], $form_state['values']['user_creation'], $form_state['values']['notes']);
+    if (isset($form_state['values']['clid']) && $form_state['values']['clid']) {
+      db_query("UPDATE {support_client} SET name = '%s', path = '%s', status = %d, integrate_email = %d, server_name = '%s', server_username = '%s', server_password = '%s', mailbox = '%s', mailfrom = '%s', protocol = %d, extra = '%s', port = %d, autoassign = '%s', autosubscribe = '%s', thread_subject = %s, domains = '%s', user_creation = '%s', notes = '%s' WHERE clid = %d", check_plain($form_state['values']['name']), $form_state['values']['path'], $form_state['values']['status'], $form_state['values']['integrate_email'], $form_state['values']['server_name'], $form_state['values']['server_username'], $form_state['values']['server_password'], $form_state['values']['mailbox'], $form_state['values']['mailfrom'], $form_state['values']['protocol'], $form_state['values']['extra'], $form_state['values']['port'], $form_state['values']['autoassign'], $form_state['values']['autosubscribe'], $form_state['values']['thread_subject'], $form_state['values']['domains'], $form_state['values']['user_creation'], $form_state['values']['notes'], $form_state['values']['clid']);
+      drupal_set_message(t('Client %client updated.', array('%client' => $form_state['values']['name'])));
     }
     else {
-      drupal_set_message(t('Client %client updated.', array('%client' => $form_state['values']['name'])));
+      db_query("INSERT INTO {support_client} (name, path, status, integrate_email, server_name, server_username, server_password, mailbox, mailfrom, protocol, extra, port, autoassign, autosubscribe, thread_subject, domains, user_creation, notes) VALUES('%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, '%s', '%s', '%d', '%s', '%s', '%s')", check_plain($form_state['values']['name']), $form_state['values']['path'], $form_state['values']['status'], $form_state['values']['integrate_email'], $form_state['values']['server_name'], $form_state['values']['server_username'], $form_state['values']['server_password'], $form_state['values']['mailbox'], $form_state['values']['mailfrom'], $form_state['values']['protocol'], $form_state['values']['extra'], $form_state['values']['port'], $form_state['values']['autoassign'], $form_state['values']['autosubscribe'], $form_state['values']['thread_subject'], $form_state['values']['domains'], $form_state['values']['user_creation'], $form_state['values']['notes']);
+      drupal_set_message(t('Client %client added.', array('%client' => $form_state['values']['name'])));
     }
   }
   // clean page cache and block cache
