diff --git a/setup.php b/setup.php
index 85de0542..886d523f 100644
--- a/setup.php
+++ b/setup.php
@@ -782,6 +782,13 @@ if(isset($_POST['add_database'])){
$password = $_POST['password'];
$config_base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']);
+ // Test database connection before writing it to config.php
+ try {
+ mysqli_connect($host, $username, $password, $database);
+ } catch (Exception $e) {
+ exit("Database connection failed - please check and try again
$e");
+ }
+
$new_config = array();
$new_config[] = "