title: PHP/MySQL update only when user clicks a button
body: <p>Okay so, I'm having this bug with my login system, it says "Wrong username or password" when I just open the login.php page.</p>
  
  <p>And I would like to know how do I make it so it checks my username and password only when I click the login button and not when I just load the page</p>
  
  <p>My button is an <code>&lt;input&gt;</code> and I tried adding <code>onclick="checklogin()"</code>  and this was the checklogin function:</p>
  
  <pre><code>$username=$_POST['username'];
  $password=$_POST['password'];
  
  function checklogin()
  {
      $sql="SELECT * FROM `users` WHERE `username`='". $username ."' and `password`='". md5($password) ."'";
      $result=mysql_query($sql);
  
      $count=mysql_num_rows($result);
  
      if($count==1){
          session_register("username");
          session_register("password"); 
          header("location:login_success.php");
      }else{
          echo "Wrong username or password.";
      }
  }
  </code></pre>
  
  <p>But nothing happens... Page just refreshes even if  I get password/name right and even if I get it wrong... But when  I remove the "function checklogin()" (its content still remains, just removing the function tag) and the onclick attribute it will work but as I said in the beggining, it will say "Wrong password or username" when I just loaded up the page</p>
  
  <p>So to make this shorter how do I make it so this login check is only applied when the user clicks the button (<code>&lt;input type="submit" name="submit" value="login"/&gt;</code>)</p>