title: user already exists in database message not displayed with php
body: <p>I'm creating a register for the user. If the user name already exists in the database the program needs to show a message. It works, but I don't know why the program does'nt show the message the first time. For example, if the name michael is already registered and I try to put again the message "user name already exists" is not displayed. But if I try again, then is displayed. Or if I try with another name already registered, is displayed. But not the first time, only the second and after. Could you help me please.</p>
  
  <pre><code>&lt;?php
  session_start();
  if(!isset($_SESSION['$k'])) {
      $_SESSION['$k'] = false;
  }
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link rel="stylesheet" href="register.css"&gt;
      &lt;title&gt;Word Games Register&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form action="" method="POST"&gt;
          &lt;a href="../index.php"&gt;&lt;img src="../img/close.png" /&gt;&lt;/a&gt;
          &lt;h2&gt;REGISTRARSE&lt;/h2&gt;
          &lt;input type="text" placeholder="Usuario" name="user"&gt;
          &lt;input type="password" placeholder="Contraseña" name="password"&gt;
          &lt;input type="text" placeholder="E-mail" name="email"&gt;
          &lt;?php
              if($_SESSION['$k']) {
                  echo '&lt;h5 id="mensaje"&gt;El usuario ya existe&lt;/h5&gt;';
                  unset($_SESSION['$k']);
              }
          ?&gt;
          &lt;input type="submit" value="Enviar" name="btn"&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  if(isset($_POST['btn'])){
      $user = $_POST['user'];
      $pass = $_POST['password'];
      $email = $_POST['email'];
  
      $link = mysqli_connect("localhost", "root", "") or die ("Error       conectando al servidor" . mysqli_error());
      mysqli_select_db($link, "wordgames") or die ("Error seleccionando la base de datos" . mysqli_error());
      mysqli_query($link, "SET NAMES 'utf8'");
  
      $resultado = mysqli_query($link, "select * from usuario where usuario='$user'") or die ("Error en la consulta" . mysqli_error());
      $filas = mysqli_num_rows($resultado);
      if($filas &gt; 0){
          $_SESSION['$k'] = true;
      } else{
          mysqli_query($link, "insert into usuario values (NULL, '$user', '$pass', '$email')") or die ("Error en la consulta". mysqli_error());
          mysqli_close($link);
          header("location:../index.php");
      }
  
  
  }
  ?&gt;
  </code></pre>