title: insert users into mysqli database with errors in form
body: <p>I have this registration form to register new users, I have some code to Verify that user is not registered before, but it looks like the code is ignoring all the verification code and after register button the user logged in directly but not inserted in the database!!! this is a big mistake I got
  my code </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  $title="Registeration";
  require_once './template/header.php';
  require_once 'config/app.php';
  require_once 'config/database.php';
  if(isset($_SESSION['logged_in'])){
      echo "&lt;script&gt;location.href = 'index.php'&lt;/script&gt;";
  }
  
  $errors=[];
  $email = '';
  $name = '';
  $phone = '';
  $city = '';
  
  // Check user entries
  
  if($_SERVER['REQUEST_METHOD'] == 'POST'){
      $email = mysqli_real_escape_string($mysqli, $_POST['email']);
      $phone = mysqli_real_escape_string($mysqli, $_POST['phone']);
      $city = mysqli_real_escape_string($mysqli, $_POST['city']);
      $name = mysqli_real_escape_string($mysqli, $_POST['name']);
      $password = mysqli_real_escape_string($mysqli, $_POST['password']);
      $password_confirmation = mysqli_real_escape_string($mysqli, $_POST['password_confirmation']);
  
      if(empty($email)){array_push($errors, "Email is Required");}
      if(empty($phone)){array_push($errors, "Phone is Required");}
      if(empty($city)){array_push($errors, "City is Required");}
      if(empty($password)){array_push($errors, "Passowrd is Required");}
      if(empty($name)){array_push($errors, "name is Required");}
      if(empty($password_confirmation)){array_push($errors, "Confirmation is Required");}
  
      //check password
      if($password != $password_confirmation){
          array_push($errors, "Password Dosn't match");
      }
  
      // check if user exists
      if(!count($errors)){
          $userExist = $mysqli-&gt;query("select id, email, phone, name from users where email ='$email' and phone ='$phone' and name ='$name'  limit 1");
          if($userExist-&gt;num_rows){
              array_push($errors, "user registered before");
          }
      }
  
      //Creat New User
  
      if(!count($errors)) {
          $password = password_hash($password, PASSWORD_DEFAULT);
          $query = "INSERT INTO users (email, phone, name, password, city) VALUES ('$email','$phone', '$name', '$password','$city')";
          $mysqli-&gt;query($query);
          //register in session
          $_SESSION['logged_in'] = true;
          $_SESSION['user_id'] = $mysqli-&gt;insert_id;
          $_SESSION['user_name'] = $name;
          $_SESSION['sucess_message'] = "Welcome to our Site!, $name";
          $_SESSION['user_email'] = $email;
           //redirect user.
          header('location: index.php');
          //echo "&lt;script&gt;location.href = 'index.php'&lt;/script&gt;";
      }
  }
  
  ?&gt;
  &lt;div id=register&gt;
      &lt;div align="right" class="container"&gt;
          &lt;h4 class="card-block text-center mt-4 text-info " &gt; أهلا وسهلاً بك كمستخدم جديد  &lt;/h4&gt;
          &lt;br&gt;
          &lt;h5 align="center" class="lead text-success font-weight-bold"&gt; إذا ممكن تعبي البيانات اللي تحت حتى نفتح لك حساب جديد &lt;/h5&gt;
          &lt;hr&gt;
          &lt;?php include 'template/errors.php' ?&gt;
          &lt;form action="" method="post"&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="name"&gt; اسم المستخدم &lt;/label&gt;
                  &lt;input type="text" name="name" class="form-control" placeholder="اسمك"  id="name" value="&lt;?php echo $name?&gt;"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="email"&gt; بريدك الإلكتروني &lt;/label&gt;
                  &lt;input type="email" name="email" class="form-control" placeholder="بريدك الإلكتروني"  id="email" value="&lt;?php echo$email?&gt;"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="city"&gt; المنطقة &lt;/label&gt;
                  &lt;input type="text" name="city" class="form-control" placeholder="المنطقة"  id="city" value="&lt;?php echo $city?&gt;"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="phone"&gt; رقم التواصل (رقم تواصلك للإعلانات اللي بتضيفها) &lt;/label&gt;
                  &lt;input type="number" name="phone" class="form-control" placeholder="رقم التواصل"  id="phone" value="&lt;?php echo$phone?&gt;"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="password"&gt; كلمة المرور &lt;/label&gt;
                  &lt;input type="password" name="password" class="form-control" placeholder="كلمة المرور"  id="password"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"  &gt;
                  &lt;label for="password_confirmation"&gt; تأكيد كلمة المرور &lt;/label&gt;
                  &lt;input type="password" name="password_confirmation" class="form-control" placeholder="تأكيد كلمة المرور"  id="password_confirmation"&gt;
              &lt;/div&gt;
              &lt;div class="form-group" &gt;
                  &lt;button class="btn btn-success" &gt; تسجيل &lt;/button&gt;
                  &lt;a href="login.php"&gt; عندك حساب معانا؟ &lt;/a&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php
      include 'template/footer.php';
  ?&gt;</code></pre>
  </div>
  </div>
  </p>