如果PHP上有2个文本字段的语句

时间:2016-10-15 12:37:00

标签: php

我正在从输入表单中发布用户名和密码,并使用它们检查我的数组是否在那里。如果两者都存在,我想回显成功消息,如果不是,我想输出错误消息。

我的成功部分if语句正在工作但我的错误消息在我尝试时没有输出。有人可以帮助我。

<html>
<head>
    <title> Half Term Project 3 </title>
</head>
<body>
    <div align="center">
    <h2> Half Term Project 3 </h2>
    <?

    $errormessage = "log in details incorrect";
    $successmessage = "You are logged in";

    $userNames[0]="Carolyne"; 
    $userNames[1]="Dave"; 
    $userNames[2]="Sharonda"; 
    $userNames[3]="Milton"; 
    $userNames[4]="Macie"; 
    $userNames[5]="Harriette";
    $userNames[6]="Gianna"; 
    $userNames[7]="Grant"; 
    $userNames[8]="Caryl"; 
    $userNames[9]="Genna"; 
    $userNames[10]="Carl";

    $passwords[0]="password"; 
    $passwords[1]="123456"; 
    $passwords[2]="master"; 
    $passwords[3]="1234"; 
    $passwords[4]="qwerty"; 
    $passwords[5]="12345"; 
    $passwords[6]="dragon"; 
    $passwords[7]="mustang"; 
    $passwords[8]="baseball"; 
    $passwords[9]="football"; 
    $passwords[10]="letmein";

    $enteredusername = $_POST["username"];
    $enteredpassword = $_POST["password"];


    for($x=0; $x<=10; $x++){
        if($enteredusername == $userNames[$x]){
            if($enteredpassword == $passwords[$x]){
            echo $successmessage;
            }
            else {
                echo $errormessage;
            }
        }
        else echo $errormessage;            
    }




    ?>
    </div>
</body>

3 个答案:

答案 0 :(得分:1)

我希望您以这种方式重写代码:

$user_found = false;
for($x=0; $x<=10; $x++){
    if($enteredusername == $userNames[$x] && $enteredpassword == $passwords[$x]) {
        $user_found = true;
        break;
    }
}
if($user_found){
    echo $successmessage;
}
else {
    echo $errormessage;
}

实现同样目的的另一种方法是使用in_array函数:

if (in_array($enteredusername, $userNames) 
    && in_array($enteredpassword, $passwords)) {
    echo $successmessage;
} else {
    echo $errormessage;
}

答案 1 :(得分:0)

***

&#13;
&#13;
<?php

for($x=0; $x<=10; $x++)
{
  if(($enteredusername == $userNames[$x]) && ($enteredpassword == $passwords[$x]))
{
    $message = "success";
    break;
}
else
{
$message = "failure";
}
}
echo $message;

?>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

看看这对你有帮助吗?

<html>
<head>
    <title> Half Term Project 3 </title>
</head>
<body>
    <div align="center">
    <h2> Half Term Project 3 </h2>
    <?php

    $errormessage = "log in details incorrect";
    $successmessage = "You are logged in";

    $userNames[0]="Carolyne"; 
    $userNames[1]="Dave"; 
    $userNames[2]="Sharonda"; 
    $userNames[3]="Milton"; 
    $userNames[4]="Macie"; 
    $userNames[5]="Harriette";
    $userNames[6]="Gianna"; 
    $userNames[7]="Grant"; 
    $userNames[8]="Caryl"; 
    $userNames[9]="Genna"; 
    $userNames[10]="Carl";

    $passwords[0]="password"; 
    $passwords[1]="123456"; 
    $passwords[2]="master"; 
    $passwords[3]="1234"; 
    $passwords[4]="qwerty"; 
    $passwords[5]="12345"; 
    $passwords[6]="dragon"; 
    $passwords[7]="mustang"; 
    $passwords[8]="baseball"; 
    $passwords[9]="football"; 
    $passwords[10]="letmein";

    $enteredusername = "Carolyn";
    $enteredpassword = "password";
    $is_exist = false;

    for($x=0; $x<=10; $x++){
        if($enteredusername == $userNames[$x] && $enteredpassword == $passwords[$x]){
            $is_exist = true;
            break;
        }
    }

    if($is_exist) echo $successmessage;
    else echo $errormessage;

    ?>
    </div>

请注意:代码中的<?php ?>标记错误。