php script execute when submit button is click
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/8028957/headers-already-sent-by-php">Headers already sent by PHP</a>  </p>
</blockquote>



<p>Greets All...
I am new to PHP and i am trying to make a login page .the problem which i am facing is the 
header in If condition gives me error and so i cant redirect to the next page if $reg and $password matches with that in database.
secondly do you think that its the best way which i am doing in this code?
sorry for my bad english
Here is my code..</p>

<pre><code>&lt;?php

$reg=$_POST["reg"];
$password=$_POST["pwrd"];

$result=mysql_query("Select * from student where reg='$reg' and password='$password'");



if(!$result){
    die('errrrrrrrrrrrrror'.mysql_error());
}



while ($row = mysql_fetch_array($result)) {
    $db_reg=$row['reg'];
    $db_password=$row['password'];

}
if(isset($_POST['login'])){

 if($reg!=$db_reg || $password!=$db_password){
   echo 'login failed';    

}
//else if($reg=="" || $password!=""){
//  echo 'empty';
//}  
    else if($reg==$db_reg &amp;&amp; $password==$db_password){

    header("Location:welcome.php");
    //echo 'weclome here'." ".$reg;


    }

}
?&gt;
</code></pre>
