title: PHP Login script does not work
body: <p>I have been trying to figure this out for a while now. I don't know why but the script wont work after I click the login button. I think that the mistake is probably in foreach.</p>
  
  <pre><code>if (isset($_POST['submit']) AND (!empty($_POST['username']) AND (!empty($_POST['password'])))) {
      $username = $_POST['username'];
      $password = $_POST['password'];
      $pass = hash('sha256',$password);
  
      $select = $db-&gt;prepare("SELECT * FROM users WHERE username= :username AND password= :password");
      $select-&gt;bindParam(':username', $username);
      $select-&gt;bindParam(':password', $password);
      $select-&gt;execute();
      $result = $select-&gt;fetchAll();  
  
      foreach ($result as $data) {
          if ($data['username'] == $username AND $data['password'] == $password) {
              $_SESSION['username'] = $username;
              $_SESSION['id'] = $data['id'];?&gt;
              &lt;div class="echos"&gt;
                  &lt;?php
                  echo "Login completed" . "&lt;br&gt;";
                  echo "&lt;a href='http://jezecek.nostools.cz/'&gt;Homepage&lt;/a&gt;";?&gt;
              &lt;/div&gt;
          &lt;?php
          } else {
              echo "Wrong username or password!" . "&lt;br&gt;";
              echo "&lt;a href='http://jezecek.nostools.cz/include/registration.php'&gt;Try again&lt;/a&gt;";
          }       
      }   
  } else {
      echo "Fill all fields!" . "&lt;br&gt;";
      echo "&lt;a href='http://jezecek.nostools.cz/include/login.php'&gt;Try again&lt;/a&gt;";
  }?&gt;
  </code></pre>