Index: knurl.install
===================================================================
--- knurl.install	(revision 32642)
+++ knurl.install	(working copy)
@@ -8,15 +8,10 @@
   $schema['knurl'] = array(
     'description' => t('Full to shortened URL mappings'),
     'fields' => array(
-      'tid' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'description' => t('The knurl ID'),  
-      ),
       'uid' => array(
         'type' => 'int',
         'not null' => TRUE,
-        'description' => t('Knurl user ID'),
+        'description' => t('User ID'),
       ),
       'short_url' => array(
         'type' => 'varchar',
@@ -30,12 +25,9 @@
         'description' => t('The original URL'),
       ),
     ),
-    'primary key' => array('tid'),
-    'indexes' => array(
-      'short_url' => array('short_url'),
-    ),
+    'primary key' => array('short_url'),
   );
-  
+
   return $schema;
 }
 
@@ -51,4 +43,13 @@
  */
 function knurl_uninstall() {
   drupal_uninstall_schema('knurl');
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation of hook_update_N().
+ */
+function knurl_update_6100() {
+  $ret = array();
+  db_drop_field($ret, 'knurl', 'tid');
+  return $ret;
+}
Index: knurl.module
===================================================================
--- knurl.module	(revision 32642)
+++ knurl.module	(working copy)
@@ -66,25 +66,25 @@
     'access callback'   => 'user_access',
     'access arguments' => array('access knurl'),
     'type'     => MENU_CALLBACK
-  );   
+  );
   return $items;
 }
 
 /**
  * Responds to /knurl/show to list all URL's
  * and /knurl/show/${url} for one
- */ 
+ */
 function knurl_show() {
   $output = '<p>';
   $url_limit = 75;
   $args = func_get_args();
   if (sizeof($args) == 1 ) {
-    $output .= l('show all', 'knurl/show'); 
+    $output .= l('show all', 'knurl/show');
     $res = db_query("SELECT * FROM {knurl} WHERE short_url='%s'", end($args));
-  } 
+  }
   else {
     $output .= '&nbsp;';
-    $res = pager_query("SELECT * FROM {knurl} ORDER BY tid DESC", 50);
+    $res = pager_query("SELECT * FROM {knurl} ORDER BY short_url DESC", 50);
   }
   $output .= '<table style="margin:0px;padding:0px;"><thead><tr>';
   $output .= '<th style="width:80%">Target URL</th>';
@@ -162,22 +162,32 @@
   global $user;
   $url_limit = 75;
   $target_url = $form_state['values']['url'];
+
   // generate a unique URL
   do {
     $short_url = _knurl_random_url();
   } while (!_knurl_is_unique_url( $short_url ) );
-  db_query("INSERT INTO {knurl} (uid, short_url, link) values (%d, '%s', '%s')", $user->uid, $short_url, $target_url);
 
-  if (strlen($target_url) > $url_limit) {
-    $target_url = "<br/>". wordwrap($form_state['values']['url'], $url_limit, "<br/>", true);
-  }
-  if ($_SERVER['HTTPS']) {  
-    $protocol = "https://";
+  $knurl = array(
+    'uid' => $user->uid,
+    'short_url' => $short_url,
+    'link' => $target_url,
+  );
+
+  if (drupal_write_record('knurl', $knurl)) {
+    if (strlen($target_url) > $url_limit) {
+      $target_url = "<br/>". wordwrap($form_state['values']['url'], $url_limit, "<br/>", true);
+    }
+    if ($_SERVER['HTTPS']) {
+      $protocol = "https://";
+    }
+    else $protocol = "http://";
+    drupal_get_messages();
+    drupal_set_message( t(sprintf('The URL shortcut was successfully created. <a href="/url/%s">%s/url/%s</a> now links to %s.<P>You can use the URL shortcut anywhere you would use the original URL.', $short_url, _get_server_url(), $short_url, $target_url)) );
+    return drupal_goto( sprintf('knurl/show/%s', $short_url) );
   }
-  else $protocol = "http://";
-  drupal_get_messages();
-  drupal_set_message( t(sprintf('The URL shortcut was successfully created. <a href="/url/%s">%s/url/%s</a> now links to %s.<P>You can use the URL shortcut anywhere you would use the original URL.', $short_url, _get_server_url(), $short_url, $target_url)) );
-  return drupal_goto( sprintf('knurl/show/%s', $short_url) );
+
+  drupal_set_message(t('A  database error occured. Short URL has not been created.'), 'error');
 }
 
 /**
