How to do an equal statement
How can I use an equals statement that solves this issue:
$username = //drawn from db;
$posted = //posted by user on form;
If $username = $posted
then process the form
If $username != $posted
then print error.
How 开发者_JS百科can I do this in one step?
if ($username === $posted) {
echo "welcome";
} else {
echo "go away";
}
In PHP, =
is an assignment operator. You probably meant to use two equals symbols together (==
) or three equals symbols together (===
), which are comparison operators.
Examples:
$user = "asdf"; // set the value of the $user variable to "asdf"
if($user == "asdf") { echo "The user's name is ASDF"; } // compare the value of $user to "asdf"
There are 2 ways:
- Extract the information from the database, with the $username and check wether there is an existing row
- Have variables (as you did) and compare them with
==
operator or===
if they have the same type.
you can run this
<? $q=mysql_fetch_array(mysql_query("SELECT username FROM yourtable WHERE username='".$_POST['FormUserName']."' and pass="'.$_POST['FormPassword']."' LIMIT 1"));
if($q['username']==$_POST['FormUserName'])
{echo "You LOGGED";}
?>
精彩评论