Index: trunk/cgi-bin/IPDB.pm
===================================================================
--- trunk/cgi-bin/IPDB.pm	(revision 544)
+++ trunk/cgi-bin/IPDB.pm	(revision 545)
@@ -671,15 +671,19 @@
 #	and available='y' order by ip limit 1);
 
-      $sth = $dbh->prepare("select ip from poolips where pool='$alloc_from'".
-	" and available='y' order by ip");
-      $sth->execute;
-
-      my @data = $sth->fetchrow_array;
-      $cidr = $data[0];  # $cidr is already declared when we get here!
-
-      $sth = $dbh->prepare("update poolips set custid=?,city=?,".
-	"available='n',description=?,notes=?,circuitid=?,privdata=?".
-	" where ip=?");
-      $sth->execute($custid, $city, $desc, $notes, $circid, $privdata, "$cidr");
+      if ($cidr) {
+	my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($cidr) );
+	if ($isavail eq 'n') {
+	  die "IP already allocated.  Deallocate and reallocate, or update the entry\n";
+	}
+	if (!$isavail) {
+	  die "IP is not in an IP pool.\n";
+	}
+      } else {
+	($cidr) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip",
+		undef, ($alloc_from) );
+      }
+      $dbh->do("UPDATE poolips SET custid=?,city=?,available='n',description=?,notes=?,circuitid=?,privdata=? ".
+	"WHERE ip=?", undef, ($custid, $city, $desc, $notes, $circid, $privdata, $cidr) );
+
 # node hack
       if ($nodeid && $nodeid ne '') {
@@ -688,8 +692,9 @@
       }
 # end node hack
+
       $dbh->commit;
     };
     if ($@) {
-      $msg .= ": '".$sth->errstr."'";
+      $msg .= ": $@";
       eval { $dbh->rollback; };
       return ('FAIL',$msg);
Index: trunk/cgi-bin/admin.cgi
===================================================================
--- trunk/cgi-bin/admin.cgi	(revision 544)
+++ trunk/cgi-bin/admin.cgi	(revision 545)
@@ -203,36 +203,22 @@
     $page->param(locerr => "Invalid customer location!  Go back and select customer's location.");
     goto ERRJUMP;
-  } else {
-    if ($webvar{alloctype} =~ /^.i$/) {
-      $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ".
-	"city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ".
-	"where ip='$webvar{cidr}'");
-      $sth->execute;
-      if ($sth->err) {
-	$page->param(errmsg => $sth->errstr);
-        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
-		"'$webvar{alloctype}' failed: '".$sth->errstr."'";
-      } else {
-	syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
-		"'$webvar{alloctype}'";
-	mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
-	  "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
-	  "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
-      }
-    } else {
-      my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
+  }
+
+  my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
 	$webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
 	$webvar{circid});
-      if ($retcode eq 'OK') {
-	syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
-		"'$webvar{alloctype}'";
-      } else {
-	$page->param(errmsg => $msg);
-        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
-		"'$webvar{alloctype}' failed: '$msg'";
-      }
-    } # static IP vs netblock
-
-  } # done city check
+  if ($retcode eq 'OK') {
+    syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
+	"'$webvar{alloctype}'";
+    if ($webvar{alloctype} =~ /^.i$/) {
+      mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
+	"$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
+	"Description: $webvar{desc}\n\nAllocated by: $authuser\n");
+    }
+  } else {
+    $page->param(errmsg => $msg);
+    syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
+	"'$webvar{alloctype}' failed: '$msg'";
+  }
 
 } elsif ($webvar{action} eq 'alloctweak') {
