Fixed mysqli error() calls, code tidying

It turns out mysqli requires a reference to the current database to
generate error strings, so have modified the error() calls accordingly.
Refactored authenticate.php to tidy up a little.