让我的sql插入查询在函数内工作

I have a form that passes input data to a processing page. This processing form then checks whether the email and user name already exists in the database. If they do, an error is reported, the function I am having difficulty with is, if the error reports nothing then go and execute the sql insert query otherwise echo the error. I can get most of it to work except the insert data to database. Can anyone help me see the error in my code please ?

Processing page :

<?php

session_start();
list($username,$email,$clubname, $hash) = $_SESSION['data'];
unset($_SESSION['data']);

include_once 'db_connect.php';

$usernameErr = $emailErr = "";

$password = $hash;
$databaseErr = 'cannot connect to database';


$query1 = mysqli_query($mysqli, "SELECT * FROM members WHERE email='".$email."'");

if(mysqli_num_rows($query1) > 0){
//  echo 'email already exists';

    $usernameErr = "username already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query1))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

$query2 = mysqli_query($mysqli, "SELECT * FROM members WHERE username='".$username."'");

if(mysqli_num_rows($query2) > 0){
//  echo 'username already exists';

    $emailErr = "email already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query2))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

if ($usernameErr == "" && $emailErr == "" ) {

    $sql = mysqli_query($mysqli, "INSERT INTO members (username, email, password, clubname) VALUES ('$username', '$email', '$password', '$clubname')");

    if (!mysqli_query($mysqli,$sql)) {
        die('Error: ' . mysqli_error($mysqli));
    }
    echo "1 record added";
} 
else {
    echo $usernameErr.'<br/><br/>';
    echo $emailErr.'<br/><br/>';
}

I came up with this, a simple skeleton, just edit to suit you. Don't forget to hash the password parameter.

function emailEmailExists($emall) {
   if(mysqli_num_rows($email) >= 1) {
     return 1;
   } else {
     return 0;
   }
 }

function usernameExists($username) {
  if(mysqli_num_rows($username) >= 1) {
    return 1;
  } else {
    return 0;
  }
}


function insertUser($username, $password, $email, $otherField, $otherField, ) {
  if(checkEmailExists($email) == 1) {
    echo 'Email in use!';
  } else if(usernameExists($username) == 1){
    echo 'Username in use!';
  } else {
    $insertUser = mysqli_query($yourQuery);

    if($insertUser) {
      echo 'User created!';
    } else {
      // Give error.
    }
  }
} 

Edit

How do you create you DB connection, remove your credentials and please show.