In your script there is only one thing that decides
to redirect, and that is this line:
if($userid != "" and $password != "")
No matter what happens in the database, if this line is true, you are redirected.
Think about exactly what needs to be true before you redirect.
If your username and password must be in the database,
you should check to see if they exist, and the redirection should depend on that.
If ($result=mysql_query("select * from table where username=username and password=password"))
{
yes they exist
}
else
{
#no they do not exist
};